Apparently there is no code yet in WordPress to get a users’ IP address when running the web server behind a web proxy. So I hacked my own in. This seems to be a not-too-common problem, but it may help some people. I have a solution that works, but it requires some extra code and changes to the existing code. It differentiates between a web user hitting the web server directly (as in most hosting situations, or through a transparent proxy) or hitting the web server through a reverse proxy (as in my situation). I wrote this code a year ago for another site I own that runs behind an Apache reverse proxy I run at home.
DO THIS AT YOUR OWN RISK!
BACK UP ALL YOUR FILES BEFORE YOU START MANUALLY EDITING THE WORDPRESS CODE!
For WordPress though, I took this code and then added it to a my-hacks.php file I created in the WordPress root directory. Then, I went into the WP-Admin and turned on the my-hacks.php legacy functionality under Settings, Miscellaneous.
Here’s the code:
<?php
function writeIPAddress() {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) == '') {
return $_SERVER['REMOTE_ADDR'];
}
else {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
}
?>
Then I took the function “writeIPAddress()” defined above and replaced every instance of “$_SERVER[‘REMOTE_ADDR’]” in the comments.php file where a users’ IP address is either called for or displayed/echoed. This file was the only place I saw where this IP call was set, but I could have missed some. Let me know how this works for you or if you found any other files or functions utilizing $_SERVER[‘REMOTE_ADDR’].
Leave a Reply