Use the Sina IP address library to retrieve the PHP version of the user's city and region

Source: Internet
Author: User
Tags php source code

Introduction: This is a detailed page for retrieving the PHP version of the user's city and region using the Sina IP address library. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 327018 'rolling = 'no'>

<? PHP
/* $ 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: Obtain the URL content
function ipfunfopen_url ($ URL)
{< br> 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);
}< br> 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, rule, 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 = '';
}< br> 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;
}

More articles about "using the Sina IP address library to retrieve the PHP version of the user's city and region"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/327018.html pageno: 12.

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.