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 ~, If you run on your computer, it's nothing more than 127.0.0.1. if you run on a virtual space or server, how does php get the IP address of the local machine ?? 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, it's nothing more than 127.0.0.1.
If it runs on a virtual space or SERVER, it is $ _ SERVER ["SERVER_ADDR "].
------ Solution --------------------
PHP code
Echo"
";
?>
Function GetIP ()
{
If (! Empty ($ _ SERVER ["HTTP_CLIENT_IP"])
$ Cip = $ _ SERVER ["HTTP_CLIENT_IP"];
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
$ Cip = "cannot be obtained! ";
Return $ cip;
}
Echo"
";
?>
Method 2:
Echo"
";
?>
Error_reporting (E_ERROR | E_WARNING | E_PARSE );
If ($ HTTP_SERVER_VARS ["HTTP_X_FORWARDED_FOR"])
{
$ Ip = $ HTTP_SERVER_VARS ["HTTP_X_FORWARDED_FOR"];
}
Elseif ($ HTTP_SERVER_VARS ["HTTP_CLIENT_IP"])
{
$ Ip = $ HTTP_SERVER_VARS ["HTTP_CLIENT_IP"];
}
Elseif ($ HTTP_SERVER_VARS ["REMOTE_ADDR"])
{
$ Ip = $ HTTP_SERVER_VARS ["REMOTE_ADDR"];
}
Elseif (getenv ("HTTP_X_FORWARDED_FOR "))
{
$ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
}
Elseif (getenv ("HTTP_CLIENT_IP "))
{
$ Ip = getenv ("HTTP_CLIENT_IP ");
}
Elseif (getenv ("REMOTE_ADDR "))
{
$ Ip = getenv ("REMOTE_ADDR ");
}
Else
{
$ Ip = "Unknown ";
}
Echo "your ip address is:". $ ip ."
";
?>
Method 3 (simplest ):
Echo"
";
?>
$ Iipp = $ _ SERVER ["REMOTE_ADDR"];
Echo $ iipp;
Echo"
";