Php obtains the code of the region based on the ip address.

Source: Internet
Author: User
Tags curl

First use php to obtain the ip address of the local machine, and then use the php curl function to obtain the detailed region of the user. Let's take a look at the code below;

The code is as follows: Copy code


/*
* This function is used to obtain the client and IP address.

*/
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 1,Using the QQWry. Dat IP library, we are only chubby.

Use a simple method.

Example

Example 1:

The code is as follows: Copy code

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

Example 2:

The code is as follows: Copy code

$ IpLocation = new IpLocation ('../qqwry/QQWry. Dat ');
$ Client = $ IpLocation-> getlocation ('192. 148.101.72 ');
Print_r ($ client );

The specific IP library and Iplocation files are not described here.


Method 2: use the api

Example 1,

The code is as follows: Copy code

/**
* Obtain the IP region
* 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> Main data of this site:. * </div> |', $ contents, $ rsR );
$ RsR [0] [0] = str_replace ("<div> Main site data:", "", $ rsR [0] [0]);
$ RsR [0] [0] = str_replace ("</div>", "", $ rsR [0] [0]);
Return $ rsR [0] [0];
 }


Example 2,


The following function uses the php curl function to obtain detailed regions from the network.

The code is as follows: Copy code

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 ("/Main site data of www.111cn.net: (. *)/I", $ jgarray [1], $ ipp );

Return "<div class =" global_widht global_zj zj "style =" background: none repeat 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; welcome from & nbsp; <B> ". $ ipp [1]. "</B> & nbsp; friends! </Div> ";

}

Echo lazdf (GetIP (); // output ip address

Welcome to Changsha

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.