Use the SINAIP address library to obtain the PHP version of the user's city and region

Source: Internet
Author: User
Use the SINAIP address library to retrieve the PHP version of the user's city and region & lt ;? Php * $ ipreal_ip (); $ urlint.dpool.sina.com. cniplookupiplookup. php? Formatjs & amp; ip. $ ip; $ contentfopen_url ($ url); $ Expcountr borrow 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;
}

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.