PHP obtains the User IP address the relatively good method _php skill

Source: Internet
Author: User

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.

A function is provided below:

Copy Code 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;
}

How to use:
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 real 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 the true IP value that getenv ("Http_x_forwarded_for") takes.

If the above else if (getenv ("http_x_forwarded_for")) gets a value that is null (that is, no proxy server is used), the following $ip = getenv ("Http_x_forwarded_for") is not executed;

In this case, you have confirmed that the client is not using a proxy server, thereby passing
Copy Code code as follows:

else if (getenv ("REMOTE_ADDR"))
$ip = getenv ("REMOTE_ADDR");

These two lines of statements obtain the IP address of the client and also the true IP address.

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.