This article describes how to use PHP to obtain the IP address of the client and obtain the IP address information. it is very practical. For more information, see. The code is concise and the function is very practical. here we will not talk nonsense, but we will provide it directly:
The code is as follows:
<? Php
/**
* Obtain the client IP address
* @ Param integer $ type return type 0: string, 1: long
* @ Return string | long
*/
Function getClientIp ($ type = 0 ){
$ Ip = NULL;
If (isset ($ _ SERVER ['http _ X_FORWARDED_FOR ']) {
$ Arr = explode (',', $ _ SERVER ['http _ X_FORWARDED_FOR ']);
$ Pos = array_search ('Unknown ', $ arr );
If (false! ==$ Pos) unset ($ arr [$ pos]);
$ Ip = trim ($ arr [0]);
} Elseif (isset ($ _ SERVER ['http _ CLIENT_IP ']) {
$ Ip = $ _ SERVER ['http _ CLIENT_IP '];
} Elseif (isset ($ _ SERVER ['remote _ ADDR ']) {
$ Ip = $ _ SERVER ['remote _ ADDR '];
}
$ Long = sprintf ("% u", ip2long ($ ip ));
$ Ip = $ long? Array ($ ip, $ long): array ('0. 0.0.0 ', 0 );
Return $ ip [$ type];
}
/**
* Obtain IP information
* @ Param string | long $ ip address
* @ Return array
*/
Function getIpInfo ($ ip)
{
If (is_long ($ ip )){
$ Ip = long2ip ($ ip );
}
$ Api = 'http: // ip.taobao.com/service/getipinfo.php ';
$ Ret = file_get_contents ($ api .'? Ip = '. $ ip );
$ Ret = json_decode ($ ret, true );
If ($ ret ['code'] = 0 ){
Return $ ret ['data'];
}
Return array ();
}
The above is all the content of this article. I hope you will like it.