Yii2: ip address behind load balancer

Created on 11 Jun 2015  路  7Comments  路  Source: yiisoft/yii2

function getUserIP() returns local ip address if the servers are behind load balancer (i am using aws)

    public function getUserIP()
    {
        return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
    }

i need to change it to each time i upgrade my framework

return (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : null;

is there any way i can override the function in my config file or somewhere so i can still use it for logging purpose.

enhancement

Most helpful comment

Yes.

  1. Create your own class that extends from \yii\web\Request.
  2. Override getUserIP().
  3. In the config, specify new class as request component.

All 7 comments

Yes.

  1. Create your own class that extends from \yii\web\Request.
  2. Override getUserIP().
  3. In the config, specify new class as request component.

or overwrite $_SERVER['REMOTE_ADDR'] in index.php

hello, what do you think if we implement this functionality like in symphony ?

Via separate method? Yes, that's OK.

@samdark I suggest to reimplement getUserIP which should use trustedHeaders property of request object and make docs how it influence to security.

related to #10854 and #13050

closing as duplicate of #10854

Was this page helpful?
0 / 5 - 0 ratings