How does php obtain the local IP address? O (Partition _ partition) O

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

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.