Jump to content

CDN real IP address


fireman28

Recommended Posts

Hello all,

 

My site is behind a CDN (incapsula) but some of my stats are worthless because it acts as a reverse proxy. All I can get is the IP address of the perimeter. They (Incapsula people) have published some generic code and instructions but I don´t know where in PS 1.6 I can put it. There also seems to be a small mod in line 13

 

Could any of you Gurus give me some assistance? It may be simple for someone who knows, I don´t want to be poking around and quite possibly mess things up.

 

Many thanks in advance.

 

Cheers!

 

Aldo

 

 

 The code is: 

<?php
/**
 * This file should be included at the beginning of your PHP code
 *
 * It changes the value of $_SERVER['REMOTE_ADDR'], to the value provided in the Incap-Client-IP header.
 * If such a value is not provided, or is not valid - no change is made.
 */

//name of HTTP header with the initial client IP address
define('HEADER_NAME','HTTP_INCAP_CLIENT_IP');

try {

//stop process if there is no header
if (empty($_SERVER[HEADER_NAME])) throw new Exception('No header defined', 1);

//validate header value
if (function_exists('filter_var')) {
$ip = filter_var($_SERVER[HEADER_NAME], FILTER_VALIDATE_IP);
if (false === $ip) throw new Exception('The value is not a valid IP address', 2);
}
else {
$ip = trim($_SERVER[HEADER_NAME]);
if (false === preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip)) throw new Exception('The value is not a valid IP address', 2);
}

//At this point the initial IP value is exist and validated
$_SERVER['REMOTE_ADDR'] = $ip;
} catch (Exception $e) {}
?>

Instructions given: 

 

===Incapsula
 
Changes the value of REMOTE_ADDRESS using information for Incapsula proxy
 
This file should be included before any usage of REMOTE_ADDRESS in PHP code.
 
===Usage instructions
 
Update line 13 in the file with the HTTP header name you need.
 
 
Link to comment
Share on other sites

I suggest creating override/class/FrontController.php with the following:

<?php

class FrontController extends FrontControllerCore
{
    public function init()
    {
        /**
        * This file should be included at the beginning of your PHP code
        *
        * It changes the value of $_SERVER['REMOTE_ADDR'], to the value provided in the Incap-Client-IP header.
        * If such a value is not provided, or is not valid - no change is made.
        */

        //name of HTTP header with the initial client IP address
        define('HEADER_NAME','HTTP_INCAP_CLIENT_IP');

        try {

        //stop process if there is no header
        if (empty($_SERVER[HEADER_NAME])) throw new Exception('No header defined', 1);

        //validate header value
        if (function_exists('filter_var')) {
        $ip = filter_var($_SERVER[HEADER_NAME], FILTER_VALIDATE_IP);
        if (false === $ip) throw new Exception('The value is not a valid IP address', 2);
        }
        else {
        $ip = trim($_SERVER[HEADER_NAME]);
        if (false === preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip)) throw new Exception('The value is not a valid IP address', 2);
        }

        //At this point the initial IP value is exist and validated
        $_SERVER['REMOTE_ADDR'] = $ip;
        } catch (Exception $e) {}

        parent::init();
    }
}

Hopefully, this will execute the code before every page in the Front Office. Remember to go to Advanced Parameters > Performance and then click the "Clear cache" button (or manually delete cache/class_index.php) so PrestaShop can find the override.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...