Home > Code Snippets > function getIpBehindProxy

function getIpBehindProxy

We were worried, about all the comments on kerala online, being marked as spam by the akismet plugin. When on detailed examination, we found that the basic problem was that wordpress was logging only the immediate downsteam ip as the remote address, well ours was a bit confusing setup, but to handle the traffic we needed it that way.

A search for wordpress behind reverse proxy, landed me to the wordpress support page. In fact the 5th entry on that page is done by Gopka, who is the lead on this project from Saturn.

We started to correct the remote address by overriding the global variable making slight changes to the wp-config, such that we will not accidentally overwrite the changes while upgrading wordpress. Well the code


if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        
$list explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
        
$_SERVER['REMOTE_ADDR'] = $list[0];
  }


when added to the wp-config, it started to log the first IP, and that would be mostly private IPs if the request was from organisations where internet was shared through proxies or using NAT. The case was same at our office, so we had to find the first public ip from the list of IPs and the code for function getIpBehindProxy was the out come.


<?php
 
function getIpBehindProxy() {
         global 
$_SERVER;
         
$privip=true;
         
$IpBehindProxy $_SERVER["REMOTE_ADDR"];
 
         
$remote = array();
         
$comes_from=array("HTTP_VIA""HTTP_X_COMING_FROM""HTTP_X_FORWARDED_FOR","HTTP_X_FORWARDED","HTTP_COMING_FROM""HTTP_FORWARDED_FOR","HTTP_FORWARDED");
         foreach (
$comes_from as $value) {
                 if (
preg_match_all("/([0-9]{1,3}\.){3,3}[0-9]{1,3}/",$_SERVER[$value],$remote_temp)) {
                         
$remotearray_merge($remote,$remote_temp[0]); //     Fish out IP match if ereg returns a value
                 
}
         }
         
$remote[]=$_SERVER["REMOTE_ADDR"];
 
   foreach(
$remote as $ip){
         if(!
$privip) continue;
         
$privip=false;
         if (
ereg("^192\.168\.[0-9]{1,3}\.[0-9]{1,3}",$ip,$remote_temp)) {
                 
$privip=true;
         }
 
         if (
ereg("^172\.([0-9]{1,3}\.){2}[0-9]{1,3}",$ip,$remote_temp)) {
                 if (
$remote_temp[1]>=16 && $remote_temp[2]<32 ) {
                         
$privip=true;
                 }
         }
 
         if (
ereg("^10\.([0-9]{1,3}\.){2}[0-9]{1,3}",$ip,$remote_temp)) {
                 
$privip=true;
         }
         if (
ereg("^127\.([0-9]{1,3}\.){2}[0-9]{1,3}",$ip,$remote_temp)) {
                 
$privip=true;
         }
 
         if(!
$privip)
           
$IpBehindProxy $ip;
   }
    return 
$IpBehindProxy;
}
 


Once this was added to the wp-config, and used to over write the global $_SERVER['REMOTE_ADDR'], all our problems went away.

For easiness of others who might get interested in function getIpBehindProxy, download is provided. function getIpBehindProxy (755)

  1. No comments yet.
  1. No trackbacks yet.

− one = one