Misappropriation of Sina IP address library access to the user's city and region PHP version

Source: Internet
Author: User
Borrow Sina IP Address library to fetch the user's city and region PHP version

/* $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, from '. $matche [1]. $matche [2]. $matche [3]. ' Friends. ';
}elseif (Preg_match ('/"ret":-1, "IP": "(. *?)" /', $content, $matche)) {
Echo ' You are using LAN, LAN IP '. $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 Get URL Content
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)
{
??? Conversion encoding, converting 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);
??????????????? $code 2 = Base_convert (substr ($STR, 4), 16, 10);
?
??????????????? $c = Chr ($code). Chr ($code 2);
??? ??? ??? ??? $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 ');
?
??????????? /* Take the first non-unknown valid IP string in X-forwarded-for */
??????????? 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.