PHP gets the customer's real IP address

Source: Internet
Author: User

if (getenv (' http_client_ip ') && strcasecmp (getenv (' http_client_ip '), ' unknown ')
{
$PHP _ip = getenv (' http_client_ip ');
}
ElseIf (getenv (' http_x_forwarded_for ') && strcasecmp (getenv (' http_x_forwarded_for '), ' unknown ')
{
$PHP _ip = getenv (' http_x_forwarded_for ');
}
ElseIf (getenv (' remote_addr ') && strcasecmp (getenv (' remote_addr '), ' unknown ')
{
$PHP _ip = getenv (' remote_addr ');
}
ElseIf (Isset ($_server[' remote_addr ']) && $_server[' remote_addr '] && strcasecmp ($_server[' Remote_ ADDR '], ' unknown ')
{
$PHP _ip = $_server[' remote_addr '];
}
Preg_match ("/[d.") {7,15}/", $PHP _ip, $ipmatches);
$PHP _ip = $ipmatches [0]? $ipmatches [0]: ' Unknown ';
/**
The function getenv () is the GET environment variable. Environment variable: HTTP_CLIENT_IP is the IP that gets the client. But it's possible that someone else has access to your program through a proxy. Then you need to use the environment variables:
It's http_x_forwarded_for. including getenv (' REMOTE_ADDR ') $_server[' REMOTE_ADDR '] are all to obtain the IP of others. Anyway Touch luo. It's workpiece to meet the one that can get it.
*/

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.