Wordpress display visitor comment IP address program

Source: Internet
Author: User

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.

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.