PHP can be used to obtain the real IP address of the client. For more information, see. After complicated judgment and calculation, the IP address retrieval function
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 ');
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 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.