Comments report inaccurate IP address


Wordpress comments are logged with the wrong IP address. They are logged as coming from the local host.

I researched this, and found some code to append to the wp-config.php file. This had the curious effect of logging different but incorrect IP addresses. I’d leave test comments from the same place and wordpress would log two different (but equally wrong) addresses.

Is this a varnish issue? is there a simple remedy?


There is weird legalities involved in some situations with gathering ips now. Consider them before changing this, as it has little value to you to actually save them. Those incorrect ips are probably correct , but protected. This is what you add to the wpconfig file:
// Use X-Forwarded-For HTTP Header to Get Visitor's Real IP Address if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { $http_x_headers = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] );


Thanks, Jbaer.

For some reason that code broke my site, but I found a similar piece of code that seemed to work … then I realized it was returning Cloudflare IP addresses (my site is behind the cloudflare cdn).

Then I found code on the Wordpress help pages meant to deal specifically with Cloudflare:

if ( isset( $_SERVER[“HTTP_CF_CONNECTING_IP”] ) ) {

This works.

I realize there are GDPR issues with logging IP addresses. I’m planning to wait and see how the laws actually get interpreted and enforced before jumping through too many hoops. In the mean time, if the comments are going to log IP addresses, I’d rather them log the real ones.