How does PHP get the IP address of this machine? O (∩_∩) o

Source: Internet
Author: User
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 "
";
  • Contact Us

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.