PHP to obtain the user's IP address a relatively good way

Source: Internet
Author: User

  This article mainly introduces PHP to obtain the User IP address method, the need for friends can refer to the following

REMOTE_ADDR can only obtain IP settings that are set in the visitor's local connection, such as 10 of its own on a university campus network. X.xxx. XXX Series IP, and this function to get the IP address of the LAN gateway exit, if the visitor uses proxy server, will not obtain the proxy server's IP, but gets the visitor gateway real IP. If you apply this function to a Web page that is restricted to IP access, someone else cannot access the page even though the proxy server in the IP Access section is restricted.     below provides a function:  code as follows: <?php   //define a function getip ()   function getip ()   {  Global $ip ;    if (getenv ("Http_client_ip"))   $IP = getenv ("http_client_ip");  else if (getenv ("Http_x_ Forwarded_for "))   $IP = getenv (" http_x_forwarded_for ");  else if (getenv (" REMOTE_ADDR "))   $IP = getenv (" REMOTE_ADDR ");  else  $ip =" Unknow ";    return $IP; }   //Use Method:  echo getip ();    ?>    getenv ("REMOTE_ADDR") is used to obtain the IP address of the client, but if the client is accessed using a proxy server, then the IP address of the proxy server is taken, not the true client IP Address. To obtain a client's real IP address through a proxy server, use getenv ("Http_x_forwarded_for") to read it.     However, if the client is not accessed through a proxy server, the value taken with getenv ("Http_x_forwarded_for") will be empty.   Copy code code as follows: else if (getenv ("Http_x_forwarded_for"))   $IP = getenv ("Http_x_forwarDed_for ");    indicates that if getenv (" Http_x_forwarded_for ") takes a value that is not empty (that is, if the client is using a proxy server), then the variable $ip equals getenv (" http_x_ Forwarded_for ") to the true IP value.     If the above else if (getenv ("http_x_forwarded_for")) gets a null value (that is, no proxy server is used), the following $ip = getenv ("http_x_forwarded _for "); this line of statements.     In this case, the client has been confirmed not to have used a proxy server, thereby replicating code code as follows through  : else if (getenv ("REMOTE_ADDR"))   $IP = getenv ("Remote_ ADDR ");    These two line statements obtain the IP address of the client and also the true IP address.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.