Skip to content
Advertisement

request IP in Laravel application not correct

in laravel when I want get visitor IP from request()->getClientIp() it always show my server IP and offcurse it not correct, I read about this problem I think it’s related to cloudflare and laravel TrustProxies but I don’t know what should I do.

how can I get real visitor IP address?

here is my $_Server dump

array:42 [
  "REDIRECT_STATUS" => "200"
  "HTTP_X_FORWARDED_HOST" => "dpaper.ir"
  "HTTP_X_FORWARDED_PROTO" => "https"
  "HTTP_X_FORWARDED_PORT" => "443"
  "HTTP_X_FORWARDED_FOR" => "::ffff:46.225.69.114"
  "HTTP_COOKIE" => "_ga=GA1.2.199690116.1583749582; _gid=GA1.2.452860032.1584212542; remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d=eyJpdiI6ImhYWmc5OW4zTktlakRhWDRYTmpGNmc9P"
  "HTTP_ACCEPT_LANGUAGE" => "en-US,en;q=0.9,fa;q=0.8,th;q=0.7"
  "HTTP_ACCEPT_ENCODING" => "gzip, deflate, br"
  "HTTP_SEC_FETCH_USER" => "?1"
  "HTTP_SEC_FETCH_MODE" => "navigate"
  "HTTP_SEC_FETCH_SITE" => "none"
  "HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
  "HTTP_USER_AGENT" => "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
  "HTTP_UPGRADE_INSECURE_REQUESTS" => "1"
  "HTTP_CONNECTION" => "keep-alive"
  "HTTP_HOST" => "dpaper.ir"
  "PATH" => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  "SERVER_SIGNATURE" => "<address>Apache/2.4.25 (Debian) Server at dpaper.ir Port 80</address>"
  "SERVER_SOFTWARE" => "Apache/2.4.25 (Debian)"
  "SERVER_NAME" => "dpaper.ir"
  "SERVER_ADDR" => "10.11.172.198"
  "SERVER_PORT" => "80"
  "REMOTE_ADDR" => "10.11.81.123"
  "DOCUMENT_ROOT" => "/var/www/html/public"
  "REQUEST_SCHEME" => "http"
  "CONTEXT_PREFIX" => ""
  "CONTEXT_DOCUMENT_ROOT" => "/var/www/html/public"
  "SERVER_ADMIN" => "webmaster@localhost"
  "SCRIPT_FILENAME" => "/var/www/html/public/index.php"
  "REMOTE_PORT" => "35058"
  "REDIRECT_URL" => "/testi"
  "GATEWAY_INTERFACE" => "CGI/1.1"
  "SERVER_PROTOCOL" => "HTTP/1.1"
  "REQUEST_METHOD" => "GET"
  "QUERY_STRING" => ""
  "REQUEST_URI" => "/testi"
  "SCRIPT_NAME" => "/index.php"
  "PHP_SELF" => "/index.php"
  "REQUEST_TIME_FLOAT" => 1584720819.106
  "REQUEST_TIME" => 1584720819
  "argv" => []
  "argc" => 0
]

Advertisement

Answer

I found a solution for this problem, for fix the problem you should put * in TrustProxies Middleware

like this:

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array|string
     */
    protected $proxies = '*';

    /**
     * The headers that should be used to detect proxies.
     *
     * @var int
     */
    protected $headers = Request::HEADER_X_FORWARDED_ALL;

}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement