PHP gets client IP and server-side IP methods

Source: Internet
Author: User
PHP gets client IP and server-side IP methods

Client IP-related variables
1. $_server[' REMOTE_ADDR '; Client IP, which may be the IP of the user or the IP of the proxy.
2. $_server[' http_client_ip '; Proxy-side IP, may exist, can be forged.
3. $_server[' http_x_forwarded_for '; The user is the agent in which IP is used, which may exist and can be forged.

Server-side IP-related variables
1. $_server["SERVER_NAME"), need to use function gethostbyname () to obtain. This variable can be displayed correctly both on the server side and on the client.
2. $_server["Server_addr"], on the server side test: 127.0.0.1 (this is related to the Bindaddress setting value in httpd.conf). The test results on the client are correct.

Classes are as follows:

Class Getip{function ClientIP () {$cIP = getenv (' remote_addr '), $cIP 1 = getenv (' http_x_forwarded_for '); $cIP 2 = getenv (' Http_client_ip '); $cIP 1? $cIP = $cIP 1:null; $cIP 2? $cIP = $cIP 2:null;return $cIP; }function ServerIP () {return gethostbyname ($_server["SERVER_NAME"]);}} $getIP = new GetIP (); $clientIp = Getip::clientip (); $serverIp = Getip::serverip (); Echo ' Client IP is ', $clientIp, '
'; Echo ' Server IP is ', $serverIp, '
'; function GetIP () {$ip = $_server[' remote_addr '];if (!empty ($_server[' http_client_ip ')) {$ip = $_server[' http_client _ip ']; } elseif (!empty ($_server[' http_x_forwarded_for ')) {$ip = $_server[' http_x_forwarded_for ']; }return $ip;} function getip__ () {if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_ip"), "unknown") $ip = Getenv ("Http_client_ip"), Else if (getenv ("Http_x_forwarded_for") && strcasecmp (getenv ("Http_x_forwarded_ For ")," Unknown ")) $ip = getenv (" Http_x_forwarded_for "), Else if (getenv (" REMOTE_ADDR ") && strcasecmp (getenv (" Remote_addr ")," Unknown ")) $ip = getenv (" REMOTE_ADDR "), Else if (isset ($_server[' remote_addr ')) && $_server[' Remote_addr '] && strcasecmp ($_server[' remote_addr '), "unknown") $ip = $_server[' remote_addr '];else$ip = " Unknown "; return ($IP);} echo GetIP ();
  • 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.