Use the SINAIP address library to retrieve the PHP version of the user's city and region & lt ;? Php/* $ ip = real_ip (); $ url = 'http: // int.dpool.sina.com.cn/iplookup/iplookup.php? Format = js & amp; ip = '. $ ip; $ Use the SINA ip Address Library to retrieve the PHP version of the user's city and region
/* $ Ip = real_ip ();
$ Url = 'http: // int.dpool.sina.com.cn/iplookup/iplookup.php? Format = js & ip = '. $ ip;
$ Content = fopen_url ($ url );
$ Exp = '/country ":"(.*?) "," Province ":"(.*?) "," City ":"(.*?) "," District ":" "," isp ":"(.*?) "," Type/s ';
$ Content = unicodeDecode ($ content );
If (preg_match ($ Exp, $ content, $ matche )){
Echo 'hello, friend from '. $ matche [1]. $ matche [2]. $ matche [3 .';
} Elseif (preg_match ('/"ret":-1, "ip ":"(.*?) "/', $ Content, $ matche )){
Echo 'you are using a LAN, and the lan ip address is '. $ matche [1];
} Else {
Echo 'you are from Mars. ';
}*/
/**
? *
? */
Function ipFunGetCityNameById (){
??? $ Url = 'http: // int.dpool.sina.com.cn/iplookup/iplookup.php? Format = js & ip = '. ipFunReal_ip ();
??? $ Content = ipFunFopen_url ($ url );
??? $ Exp = '/country ":"(.*?) "," Province ":"(.*?) "," City ":"(.*?) "," District ":" "," isp ":"(.*?) "," Type/s ';
??? $ Content = ipFunUnicodeDecode ($ content );
??? If (preg_match ($ Exp, $ content, $ matche )){
??? ??? Return $ matche [3];
??? } Else
??? ??? Return "";
}
?
?
// Curl
Function ipFunFopen_url ($ url)
{
??? If (function_exists ('File _ get_contents ')){
??????? $ File_content = @ file_get_contents ($ url );
??? } Elseif (ini_get ('allow _ url_fopen') & ($ file = @ fopen ($ url, 'RB '))){
??????? $ I = 0;
??????? While (! Feof ($ file) & $ I ++ <1000 ){
??????????? $ File_content. = strtolower (fread ($ file, 4096 ));
??????? }
??????? Fclose ($ file );
??? } Elseif (function_exists ('curl _ init ')){
??????? $ Curl_handle = curl_init ();
??????? Curl_setopt ($ curl_handle, CURLOPT_URL, $ url );
??????? Curl_setopt ($ curl_handle, CURLOPT_CONNECTTIMEOUT, 2 );
??????? Curl_setopt ($ curl_handle, CURLOPT_RETURNTRANSFER, 1 );
??????? Curl_setopt ($ curl_handle, CURLOPT_FAILONERROR, 1 );
??????? Curl_setopt ($ curl_handle, CURLOPT_USERAGENT, 'trackback Spam check ');
??????? $ File_content = curl_exec ($ curl_handle );
??????? Curl_close ($ curl_handle );
??? } Else {
??????? $ File_content = '';
??? }
??? Return $ file_content;
}
?
Function ipFunUnicodeDecode ($ content)
{
??? // Convert the Unicode encoding to gb2312 encoding.
??? $ Pattern = '/(\ u ([\ w] {4})/I ';
??? Preg_match_all ($ pattern, $ content, $ matches );
??? If (! Empty ($ matches ))
??? {
??????? For ($ j = 0; $ j <count ($ matches [0]); $ j ++)
??????? {
??????????? $ Str = $ matches [0] [$ j];
??????????? If (strpos ($ str, '\ u') = 0)
??????????? {
??????????????? $ Code = base_convert (substr ($ str, 2, 2), 16, 10 );
??????????????? $ Code2 = base_convert (substr ($ str, 4), 16, 10 );
?
??????????????? $ C = chr ($ code). chr ($ code2 );
??? ??? ??? ??? $ C = mb_convert_encoding ($ c, "UTF-8", 'ucs-2 ');
??????????????? // $ C = iconv ('ucs-2', 'gb2312', $ c );
??? ??? ??? ??? $ Content = str_replace ($ matches [0] [$ j], $ c, $ content );
??????????? }
??????????? Else
??????????? {
??????????????? $ Content. = $ str;
??????????? }
??????? }
??? }
??? Return $ content;
}
?
Function ipFunReal_ip ()
{
??? Static $ realip = NULL;
?
??? If ($ realip! = NULL)
??? {
??????? Return $ realip;
??? }
?
??? If (isset ($ _ SERVER ))
??? {
??????? If (isset ($ _ SERVER ['http _ X_FORWARDED_FOR '])
??????? {
??????????? $ Arr = explode (',', $ _ SERVER ['http _ X_FORWARDED_FOR ']);
?
??????????? /* Obtain the first non-unknown valid IP string in X-Forwarded-*/
??????????? Foreach ($ arr AS $ ip)
??????????? {
??????????????? $ Ip = trim ($ ip );
?
??????????????? If ($ ip! = 'Unknown ')
??????????????? {
??????????????????? $ Realip = $ ip;
?
??????????????????? Break;
??????????????? }
??????????? }
??????? }
??????? Elseif (isset ($ _ SERVER ['http _ CLIENT_IP '])
??????? {
??????????? $ Realip = $ _ SERVER ['http _ CLIENT_IP '];
??????? }
??????? Else
??????? {
??????????? If (isset ($ _ SERVER ['remote _ ADDR '])
??????????? {
??????????????? $ Realip = $ _ SERVER ['remote _ ADDR '];
??????????? }
??????????? Else
??????????? {
??????????????? $ Realip = '0. 0.0.0 ';
??????????? }
??????? }
??? }
??? Else
??? {
??????? If (getenv ('http _ X_FORWARDED_FOR '))
??????? {
??????????? $ Realip = getenv ('http _ X_FORWARDED_FOR ');
??????? }
??????? Elseif (getenv ('http _ CLIENT_IP '))
??????? {
??????????? $ Realip = getenv ('http _ CLIENT_IP ');
??????? }
??????? Else
??????? {
??????????? $ Realip = getenv ('remote _ ADDR ');
??????? }
??? }
?
??? Preg_match ("/[\ d \.] {7, 15}/", $ realip, $ onlineip );
??? $ Realip =! Empty ($ onlineip [0])? $ Onlineip [0]: '0. 0.0.0 ';
?
??? Return $ realip;
}