PHP to determine the way to access IP, PHP judge IP
The example in this paper describes how PHP judges access to IP. Share to everyone for your reference. Specific as follows:
} else if (! empty ($_server ["http_x_forwarded_for"])) { $cip = $_server ["Http_x_forwarded_for"]; } else if ( ! Empty ($_server ["REMOTE_ADDR"]) { $CIP = $_server ["REMOTE_ADDR"]; } else { $
Ec (2); multiple real IP address acquisition codes are an environment variable that comes with php to determine and then obtain the real IP address of the user. Let's take a look at the one that suits you. 1: lt ;? Phpif (getenv (HTTP_CLIENT_IP) { nbsp; $ onlineipgetenv (HTTP_CLIENT_IP);} elseif (getenv (HTTP_X_FORWARDED_FOR) { nbsp; $ script ec (2 ); script
Multiple real IP address codes for getting users are an environment variable that comes with php to determine and then obtain the real I
The code is as follows:
Function GetIP (){If (! Empty ($ _ SERVER ["HTTP_CLIENT_IP"]) {$ Cip = $ _ SERVER ["HTTP_CLIENT_IP"];}Elseif (! Empty ($ _ SERVER ["HTTP_X_FORWARDED_FOR"]) {$ Cip = $ _ SERVER ["HTTP_X_FORWARDED_FOR"];}Elseif (! Empty ($ _ SERVER ["REMOTE_ADDR"]) {$ Cip = $ _ SERVER ["REMOTE_ADDR"];}Else {$
How does php obtain the local IP address ?? O (distinct _ distinct) O ~ How can I obtain the IP address of the local machine when I use the php command to execute the php file? It turns out that $ _ SERVER [] is not easy to use. thanks to O (cost _ priority) O ~ ------ Solution ------------------ if you are running on your computer, 127.0.0.1 is nothing more than 127.0.0.1. if you are running on a virtual space or SERVER, $ _ SERVER [ quot; how does php get the IP address of the local machine ??
(!fwrite ($FP, $line)) {
Die ("Write online list failed.");
}
Fclose ($FP);
}
Get people online
function Get_online_sum ()
{
if (file_exists (online_list)) {
$online _msg = file (online_list);
Return count ($online _msg);
} else {
return 0;
}
}
Get Current Login User IP
function Get_client_ip ()
{
if ($_server[' REMOTE_ADDR ']) {
$
There are many ways to obtain IP addresses in php. Next I will introduce how to use linux commands to obtain IP addresses and convert them into array output. Below I also introduce some common php IP address processing instances.
PHP obtains the Server IP Address
Run ifconfig in PHP to get the Linux Server IP address and output it as an array. The following code is used:
The Code is as follows:
Copy code
Function getServerIp () {// use ifconfig to read the Server IP addre
PHP curl counterfeit IP address and header information code instance, curlheader
Although curl is powerful, it can only forge $ _ SERVER ["HTTP_X_FORWARDED_FOR"]. For most IP address detection programs, it is difficult to forge $ _ SERVER ["REMOTE_ADDR:
First, the client. php code
Copy codeThe Code is as follows:$ Headers ['client-ip'] = '192. 103.229.40 ';$ Headers ['x-FORWARDED-FOR '] = '2017. 103.229.40 ';$ HeaderArr = array ();Foreach ($ headers as $ n => $ v ){$ HeaderArr [] = $ n. ':'. $ v
PHP obtains the server IP addressRun ifconfig in PHP to get the Linux server IP address and output it as an array. The following code is used: The code is as follows:Copy code Function getServerIp () {// use ifconfig to read the server IP address and output it as an array$ Ss = exec ('/sbin/ifconfig | sed-n's/^ *. * addr: \ ([0-9.] \ {7 ,\}\). * $/\ 1/p', $ arr );Return $ arr;}$ Ips = getServerIp ();Foreach ($ ips as $ k => $ v) {// filter IP addressesIf (substr ($ v, 127) = '
A variety of users really IP code is a PHP with the environment variables to determine the user's real IP address, well look at that paragraph for you.1:if (getenv (' http_client_ip ')) {$onlineip = getenv (' http_client_ip ');}ElseIf (getenv (' http_x_forwarded_for ')) {$onlineip = getenv (' http_x_forwarded_for ');}ElseIf (getenv (' remote_addr ')) {$onlineip = getenv (' remote_addr ');}else{$onlineip = $HTTP _server_vars[' remote_addr '];}Echo $onlineip;?>
2:function GetIP () {if (!empty ($_
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.