PHP Get client IP address explained

Source: Internet
Author: User

<?php

Http://www.cnblogs.com/chengmo/archive/2013/05/29/php.html (please refer to this blog post for details)

1. ' REMOTE_ADDR ' is the remote IP, the default from the TCP connection is the IP of the client. It is the most accurate, but only gets the direct connection to the server client IP.
If the other side through the proxy server online, it is found. Get to the proxy server IP.

such as: A->b (proxy)->c, if C through ' remote_addr ', can only get to the IP of B, get less than the IP of a. */

$ip =$_server[' remote_addr '];
Echo $ip;

2. ' Http_x_forwarded_for ', ' http_client_ip ' in order to be able to get to the most original user IP in a large network, or proxy IP address. Extend the HTTP protocol. Defines the entity header.

Http_x_forwarded_for = Clientip,proxy1,proxy2 all IPs with "," split.
HTTP_CLIENT_IP in the Advanced anonymous proxy, this represents the proxy server IP.
Since the HTTP protocol extends an entity header, and this value is trusted for the incoming side, trust the incoming party to enter it in the regular format.

The x-forwarded-for request header format is very simple:
X-forwarded-for:client, Proxy1, Proxy2
*/

function GetIP () {

if (Isset ($_server[' http_x_forwarded_for ')) {
$realip = $_server[' http_x_forwarded_for ');
} elseif (Isset ($_server[' http_client_ip ')) {
$realip = $_server[' http_client_ip ');
} else {
$realip = $_server[' remote_addr ');
}
return $realip;
}
echo GetIP ();

?>

PHP Get client IP address explained

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.