After the blog resumes over the past two days, I was excited and found that the visitor's message ip address is the proxy address instead of the real ip address. So I printed the $ _ SERVER information, we can see that $ _ SERVER ['remote _ ADDR '] is the visitor's ip address, but the result is the proxy address. This is not what we need, at the same time, it is found that the $ _ SERVER contains the HTTP_X_FORWARDED_FOR field, and wordpress uses the REMOTE_ADDR field value, which is easy to handle. You can use the following code to change the visitor address.
The code is as follows: |
Copy code |
If (isset ($ _ SERVER ['http _ X_FORWARDED_FOR ']) { $ List = explode (',', $ _ SERVER ['http _ X_FORWARDED_FOR ']); $ _ SERVER ['remote _ ADDR '] = $ list [0]; } |
Add the above code to the beginning of the wp-config.php.
Because the record formats of the X-Forwarded-For header are: client1, proxy1, and proxy2, we take the first value in the above code, however, my friends will find that the above only shows that the IP address does not show the source. For example, if you are from Changsha, Hunan, I can use multiple methods, but now I am mainly calling the api directly.
Obtain the code of the city (Sina API) based on the IP address:
The code is as follows: |
Copy code |
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/> <Title> Sina API _ blue sky </title> <Script src = "http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = js "type =" text/ecmascript "> </script> <Script type = "text/javascript"> Alert ("your current location: "+ remote_ip_info [" country "] +'' + remote_ip_info ["province"] + "province" + ''+ remote_ip_info [" city "] +" city "+" "+ remote_ip_info ["isp"]); </Script> </Head> <Body> </Body> </Html> |
This is even simpler. Even php does not need to call Sina's api directly.