PHP obtains client + server IP address and geographical location information

Source: Internet
Author: User
IP: 113. 83. ***. ** return status: 1IP range: Strat: 113.83.0.0IP range: End: 113.83.20.255 country: Chinese province: Guangdong city: Huizhou district/county: line: Telecom type: Description: [PHP] code 01/*** get client + server IP address and geographic location information Sinai... "> <LINKhref =" http ://

 

IP: 113. 83 .***.**
Returned status: 1
IP address range: Strat: 113.83.0.0
IP address range: End: 113.83.255.255
Country: China
Province: Guangdong
City: Huizhou
District/county:
Line: China Telecom
Type:
Description: [PHP] code
01 /**
02 * Obtain the client + server IP address and geographic location information Sina iplookup
03 *
04 * @ Support: QQ 910111100 (JoY)
05 * @ Time: 2012.10.11 15:50:00
06 * @ HZapi.com (http://www.php100.com /)
07 *
08 */
09 // Obtain geographic location information
10 Function iplookup ($ ip = 1 ){
11 If ($ ip) {// client
12 $ Userip = egetip_joy (); // client IP address
13 } Else {// server
14 $ Domain = $ _ SERVER ['http _ host'];
15 $ Userip = gethostbyname ($ domain );
16 }
17 // Returns the location information of Sina.
18 $ Json = @ file_get_contents ('http: // int.dpool.sina.com.cn/iplookup/iplookup.php? Format = js & ip = '. $ userip );
19 $ Patterns = array ();
20 $ Patterns [0] = '/var remote_ip_info = /';
21 $ Patterns [1] = '/;/';
22 $ Patterns [2] = '/\ Wu /';
23 $ Find = array ();
24 $ Find [0] = "";
25 $ Find [1] = "";
26 $ Find [2] = "% u ";
27 $ Json = preg_replace ($ patterns, $ find, $ json); // filter extra characters
28 $ Json_arr = json_decode ($ json, true );
29 Return $ json_arr;
30 }
31 // Obtain the IP address
32 Function egetip_joy (){
33 If (getenv ('http _ CLIENT_IP ') & strcasecmp (getenv ('http _ CLIENT_IP'), 'Unknown '))
34 {
35 $ Ip = getenv ('http _ CLIENT_IP ');
36 }
37 Elseif (getenv ('http _ X_FORWARDED_FOR ') & strcasecmp (getenv ('http _ X_FORWARDED_FOR'), 'Unknown '))
38 {
39 $ Ip = getenv ('http _ X_FORWARDED_FOR ');
40 }
41 Elseif (getenv ('remote _ ADDR ') & strcasecmp (getenv ('remote _ ADDR'), 'Unknown '))
42 {
43 $ Ip = getenv ('remote _ ADDR ');
44 }
45 Elseif (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], 'Unknown '))
46 {
47 $ Ip = $ _ SERVER ['remote _ ADDR '];
48 }
49 $ Ip = preg_replace ("/^ ([d.] +). */", "1", $ ip );
50 Return $ ip;
51 }
52 /**
53 * Simulate unescape in JS
54 *
55 * @ Support: QQ 910111100 (JoY)
56 * @ Time: 2012.09.29 15:50:00
57 * @ HZapi.com (http://www.hzapi.com /)
58 * Echo unescape ('% u4e1c % u6e56 % u82b1 % u56ed4 % u53f7 % u5c0f % u533a ');
59 */
60 Function unescape ($ str ){
61 $ Str = rawurldecode ($ str );
62 Preg_match_all ("/(? : % U. {4}) |. {4}; | & # \ d +; |. +/U ", $ str, $ r );
63 $ Ar = $ r [0];
64 Foreach ($ ar as $ k => $ v ){
65 If (substr ($ v, 0, 2) = "% u ")
66 {
67 $ Ar [$ k] = iconv ("UCS-2", "UTF-8 // IGNORE", pack ("H4", substr ($ v,-4 )));
68 }
69 Elseif (substr ($ v, 0, 3) = "")
70 {
71 $ Ar [$ k] = iconv ("UCS-2", "UTF-8", pack ("H4", substr ($ v, 3,-1 )));
72 }
73 Elseif (substr ($ v, 0, 2) = "&#")
74 {
75 Echo substr ($ v, 2,-1 )."";
76 $ Ar [$ k] = iconv ("UCS-2", "UTF-8", pack ("n", substr ($ v, 2,-1 )));
77 }
78 }
79 Return join ("", $ ar );
80 }
81  
82  
83 $ Iplookup = iplookup (); // The parameter iplookup (1) is not empty.
84  
85 Echo "IP:". egetip_joy ()."
";
86 Echo 'return status: '. $ iplookup ['ret']."
";
87 Echo 'IP range Strat: '. $ iplookup ['start']."
";
88 Echo 'IP range End: '. $ iplookup ['end']."
";
89 Echo 'Country: '. unescape ($ iplookup ['country'])."
";
90 Echo 'Province: '. unescape ($ iplookup ['Province'])."
";
91 Echo 'City: '. unescape ($ iplookup ['city'])."
";
92 Echo 'district/county:'. unescape ($ iplookup ['District '])."
";
93 Echo 'line: '. unescape ($ iplookup ['isp'])."
";
94 Echo 'type: '. $ iplookup ['type']."
";
95 Echo description: '. $ iplookup ['desc']."
";
96 // Print_r ($ iplookup );

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.