Php code for obtaining the Internet IP address of the client. Leng Feng: both methods are available. The first method must be supported by the built-in curl, and the second method is more common. The second type is recommended. Method 1: curlfunctionget_onlineip () {$ chcurl_init Cold front: both methods are available. The first method must be supported by built-in curl, and the second method is more common. The second type is recommended.
Method 1: curl
Function get_onlineip (){
$ Ch = curl_init (http://www.ip138.com/ip2city.asp );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
$ A = curl_exec ($ ch );
Preg_match (/[(. *)]/, $ a, $ ip );
Return $ ip [1];
}
Method 2: $ _ SERVER [HTTP_X_FORWARDED_FOR] to obtain the corresponding address
Function get_onlineip (){
$ Onlineip =;
If (getenv (HTTP_CLIENT_IP) & strcasecmp (getenv (HTTP_CLIENT_IP), unknown )){
$ Onlineip = getenv (HTTP_CLIENT_IP );
} Elseif (getenv (HTTP_X_FORWARDED_FOR) & strcasecmp (getenv (HTTP_X_FORWARDED_FOR), unknown )){
$ Onlineip = getenv (HTTP_X_FORWARDED_FOR );
} Elseif (getenv (REMOTE_ADDR) & strcasecmp (getenv (REMOTE_ADDR), unknown )){
$ Onlineip = getenv (REMOTE_ADDR );
} Elseif (isset ($ _ SERVER [REMOTE_ADDR]) & $ _ SERVER [REMOTE_ADDR] & strcasecmp ($ _ SERVER [REMOTE_ADDR], unknown )){
$ Onlineip = $ _ SERVER [REMOTE_ADDR];
}
Return $ onlineip;
}
Echo get_onlineip ();
?>
Bytes. The second type is recommended. Method 1: curl function get_onlineip () {$ ch = curl_init...