I'm sure you decided to do this, but I thought I would post the correct answer for future reference. I ran into the same problem (using AWS load balancers with the CodeIgniter application.) As you pointed out, it’s easy enough to get the correct IP address from the load balancer or other distributed environment using the HTTP_X_FORWARDED_FOR header. The problem is, how do we correctly implement this solution in CodeIgniter? As the previous answer follows: Write your own IP function. The problem with this is, what if ip_address () is called throughout the application? Wouldn't it be better to override this function (with the one that looks at the correct header)? CodeIgniter has a convenient mechanism for this, which is convenient:
The solution is to extend the CodeIgniter input class by creating a new class file in / application / core called MY_Input.php (MY_ is a custom prefix for extensions, you can change it in your configuration file). With the help of extensions, you can create a function for the name SAME as the original method of the class without violating anything and without editing the kernel files. CodeIgniter just uses your new method. Your extended input class will look something like this:
class MY_Input extends CI_Input { function __construct() { parent::__construct(); }
Thus, we changed the main behavior without breaking the framework, and existing calls to ip_address () throughout the application will now use your method.
As for working with a different IP address in the chain, if you are only interested in the client IP address, it does not matter. At a minimum, with AWS load balancers, the HTTP_X_FORWARDED_FOR header always contains the correct client IP address.
Oliver holmberg
source share