PHP Gets the program code of the region based on IP

Source: Internet
Author: User
Tags curl get ip

First use PHP to obtain the IP, and then use the PHP Curl function to obtain the user's detailed area. Here's a look at the code;

The code is as follows


/*
* This function is to get the client and IP

*/
function GetIP ()
{
if (!empty ($_server["HTTP_CLIENT_IP"]))
{
$cip = $_server["Http_client_ip"];
}
else if (!empty ($_server["http_x_forwarded_for"]))
{
$cip = $_server["Http_x_forwarded_for"];
}
else if (!empty ($_server["REMOTE_ADDR"]))
{
$cip = $_server["REMOTE_ADDR"];
}
Else
{
$cip = ';
}
Preg_match ("/[d.") {7,15}/", $cip, $cips);
$cip = isset ($cips [0])? $cips [0]: ' Unknown ';
Unset ($cips);
return $CIP;
}

Method one, using QQWry.Dat IP library, we only fat

In a simple way.

Using the sample

Example one:

The code is as follows

$IpLocation = new Iplocation ();
$client = $IpLocation->getlocation ();
Print_r ($client);

Example two:

The code is as follows

$IpLocation = new Iplocation ('.. /qqwry/qqwry.dat ');
$client = $IpLocation->getlocation (' 115.148.101.72 ');
Print_r ($client);

The specific IP library and Iplocation class files here do not introduce everyone Baidu search bar.

Method two, using API interface

Example One,

The code is as follows

/**
* Get IP area
* Enter description here ...
* @param unknown_type $ip
*/
function Getarea ($IP) {

$url = "http://ip168.com/ip/?ip=". $ip;
$contents = file_get_contents ($url);
Preg_match_all ('/Preg_match_all (' |<div> site main data:. *</div>| ', $contents, $rsR);
$rsR [0][0] = Str_replace ("<div> site master data:", "", $rsR [0][0]);
$rsR [0][0] = Str_replace ("</div>", "", $rsR [0][0]);
return $rsR [0][0];
}

Example Two,

The following function uses PHP's curl function to get detailed area from the Web

  code is as follows &nbs P;

function LAZDF ($IP) {

$curl = Curl_init ();

Curl_setopt ($curl, Curlopt_url, "http://www.ip138.com/ips138.asp?ip=". $ip);
curl_setopt ($curl, curlopt_returntransfer,1);
$ipdz =curl_exec ($curl);
Curl_close ($curl);
Preg_match (/<ul class= "Ul1" ><li> (. *?) </li>/i ", $ipdz, $jgarray);
Preg_match ("/www.111cn.net station main data: (. *)/I", $jgarray [1], $IPP);

return  "<div class=" global_widht global_zj zj "style=" Background:none repeat the scroll 0% 0% rgb (226, 255, 191); font-size:12px; Color:rgb (85, 85, 85); height:30px; line-height:30px; BORDER-BOTTOM:1PX Solid RGB (204, 204, 204); Text-align:left; " >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; welcome from &nbsp;<b> "$ipp [1]." </b>&nbsp; Friends! </div> ";

}

Echo lazdf (GeTIP ());/output IP

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.