PHP multiple methods to obtain the real IP address of the client _ PHP Tutorial

Source: Internet
Author: User
PHP can be used to obtain the real IP address of the client. The code for copying an IP address function after complex judgment and calculation is as follows: functiongetIP () {if (getenv (HTTP_CLIENT_IP) {$ ipgetenv (HTTP_CLIENT_IP );} elseif (getenv (function for obtaining IP addresses after complicated judgment and calculation

The code is as follows:


Function getIP (){
If (getenv ('http _ CLIENT_IP ')){
$ Ip = getenv ('http _ CLIENT_IP ');
}
Elseif (getenv ('http _ X_FORWARDED_FOR ')){
$ Ip = getenv ('http _ X_FORWARDED_FOR ');
}
Elseif (getenv ('http _ x_forwarded ')){
$ Ip = getenv ('http _ x_forwarded ');
}
Elseif (getenv ('http _ FORWARDED_FOR ')){
$ Ip = getenv ('http _ FORWARDED_FOR ');

}
Elseif (getenv ('http _ forwarded ')){
$ Ip = getenv ('http _ forwarded ');
}
Else {
$ Ip = $ _ SERVER ['remote _ ADDR '];
}
Return $ ip;
}


The simplest example of getting an IP address code

The code is as follows:


$ ReIP = $ _ SERVER ["REMOTE_ADDR"];
Echo $ reIP;


Php ip retrieval algorithm

The code is as follows:


If (getenv ('http _ CLIENT_IP ')){
$ Onlineip = getenv ('http _ CLIENT_IP ');
} Elseif (getenv ('http _ X_FORWARDED_FOR ')){
$ Onlineip = getenv ('http _ X_FORWARDED_FOR ');
} Elseif (getenv ('remote _ ADDR ')){
$ Onlineip = getenv ('remote _ ADDR ');
} Else {
$ Onlineip = $ HTTP_SERVER_VARS ['remote _ ADDR '];
}
Echo $ onlineip;


Attackers can obtain IP addresses of internal and external websites.

The code is as follows:


Function getip_out (){
$ Ip = false;
If (! Empty ($ _ SERVER ["HTTP_CLIENT_IP"]) {
$ Ip = $ _ SERVER ["HTTP_CLIENT_IP"];
}
If (! Empty ($ _ SERVER ['http _ X_FORWARDED_FOR ']) {
$ Ips tutorial = 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;
}
}
}
Return ($ ip? $ Ip: $ _ SERVER ['remote _ ADDR ']);
}
Echo getip_out ();


What algorithm does php use to obtain ip addresses? Number expression to process

The code is as follows:


$ User_IP = ($ _ SERVER ["HTTP_VIA"])? $ _ SERVER ["HTTP_X_FORWARDED_FOR"]: $ _ SERVER ["REMOTE_ADDR"];
$ User_IP = ($ user_IP )? $ User_IP: $ _ SERVER ["REMOTE_ADDR"];


Function for obtaining the client IP address in php
Php obtains the real ip address of the client.

The response code is as follows: function getIP () {if (getenv ('http _ CLIENT_IP ') {$ ip = getenv ('http _ CLIENT_IP ');} elseif (getenv (...

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.