How does PHP get the IP address of this machine?? O (∩_∩) o~
When I use PHP command to execute PHP file, how to get the IP address of this machine? The original through $_server[] bad use, first thank you O (∩_∩) o~
------Solution--------------------
If you're running on your own computer, it's 127.0.0.1.
$_server["Server_addr" if run on a virtual space or a server
------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 get!" ";
return $CIP;
}
echo "
";
?>
Method Two:
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 Three (simplest):
echo "
";
?>
$iipp = $_server["REMOTE_ADDR"];
Echo $iipp;
echo "
";