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: |
|
/* * 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 fat
Use a simple method.
Example
Example 1:
The Code is as follows: |
|
$ IpLocation = new IpLocation (); $ Client = $ IpLocation-> getlocation (); Print_r ($ client ); |
Example 2:
The Code is as follows: |
|
$ 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: |
|
/** * 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: |
|
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 |