How PHP obtains the client's real IP address

Source: Internet
Author: User
This article mainly introduces the PHP to obtain the client real IP address of the method, has a certain reference value, now share to everyone, the need for friends can refer to

PHP Gets the client IP address in four ways, these five methods are REMOTE_ADDR, Http_client_ip, Http_x_forwarded_for, Http_via, respectively.

REMOTE_ADDR is the IP of your client when it "shakes hands" with your server. If you use anonymous proxy, REMOTE_ADDR displays the IP of the proxy server.
HTTP_CLIENT_IP is the HTTP header sent by the proxy server. If "Super Anonymous proxy", the value of none is returned. Similarly, REMOTE_ADDR will be replaced with the IP of this proxy server.
$_server[' REMOTE_ADDR ']; The IP of the access side (possibly the user, possibly the proxy)
$_server[' Http_client_ip ']; Agent-side (may exist, can be forged)
$_server[' http_x_forwarded_for ']; The user is the agent in which IP is used (may exist or can be forged)

The three values differ as follows:

One, not using the proxy server situation:

REMOTE_ADDR = Your IP
Http_via = no value or no display
Http_x_forwarded_for = no value or no display

Second, the use of transparent proxy server situation: Transparent Proxies

REMOTE_ADDR = Last Proxy server IP
Http_via = Proxy Server IP
Http_x_forwarded_for = Your real IP, after multiple proxy servers, this value resembles the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

This type of proxy server also forwards your information to your Access object, and does not achieve the purpose of hiding the true identity.

Third, the use of ordinary anonymous proxy server situation: Anonymous Proxies

REMOTE_ADDR = Last Proxy server IP
Http_via = Proxy Server IP
Http_x_forwarded_for = Proxy IP, after multiple proxies, this value resembles the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Hides your real IP, but reveals to the Access object that you are using a proxy server to access them.

Iv. use of deceptive proxy servers: distorting Proxies

REMOTE_ADDR = Proxy Server IP
Http_via = Proxy Server IP
Http_x_forwarded_for = Random IP, after multiple proxy servers, this value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Tells the Access object that you used a proxy server, but fabricated a bogus random IP to spoof it instead of your real IP.

V. Use of highly anonymous proxy servers: High anonymity Proxies (Elite Proxies)

REMOTE_ADDR = Proxy Server IP
Http_via = no value or no display
Http_x_forwarded_for = no value or not displayed, after multiple proxy servers, this value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Full use of Proxy server information replaces all of your information, as if you were using the proxy server directly to access the object.

Six, obtain the client real IP address instance:

function Get_real_ip () {    $ip =false;    Client IP or NONE     if (!empty ($_server["Http_client_ip")) {        $ip = $_server["Http_client_ip"];    }    Client real IP address under multi-proxy server (possibly forged), if no proxy is used, this field is empty    if (!empty ($_server[' http_x_forwarded_for ')) {        $ips = explode (" , ", $_server[' http_x_forwarded_for ');        if ($IP) {array_unshift ($ips, $ip); $ip = FALSE;}        for ($i = 0; $i < count ($ips); $i + +) {            if (!eregi ("^ (10│172.16│192.168).", $ips [$i])) {                $ip = $ips [$i];
  
   break    ;    }}} Client IP or (last) Proxy IP     return ($ip? $ip: $_server[' remote_addr '));}
  

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

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.