May 12, 2014

Magento: X_FORWARDED_FOR not recorded on core_session data.

If your site are behind proxies/LoadBalancer. x_forwarded_for will be use to determine the where is the original request came from. But suddenly magento can't able to log this data in you core_session data. To fixed this one find Mage_Core_Model_Session_Abstract_Varien class and getValidatorData method.

In the code section below
if (isset($_ENV['HTTP_X_FORWARDED_FOR'])) {

Add an inline elseif statement
}elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {

Lets take look the original code.
public function getValidatorData()
    {
  // Some codes here ....
  if (isset($_ENV['HTTP_X_FORWARDED_FOR'])) {
    $parts[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY] = (string)$_ENV['HTTP_X_FORWARDED_FOR'];
  }

  // Some codes here ....        
  return $parts;
}

After adding the inline elseif statement section this will be output
public function getValidatorData()
{
  // Some codes here ....
  if (isset($_ENV['HTTP_X_FORWARDED_FOR'])) {
    $parts[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY] = (string)$_ENV['HTTP_X_FORWARDED_FOR'];
  }elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $parts[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY] = (string)$_SERVER['HTTP_X_FORWARDED_FOR'];
  }

  // Some codes here ....        
  return $parts;
}


NOTE/Disclamer: I don't know why magento developer didn't realize the _ENV global variable, only store data that set by server using setEvn method, and didnt event try to test and get the data in _SERVER global variable. BTW I just saw this bug on magento 1.5.X.

No comments: