LoginSignup
16
12

More than 5 years have passed since last update.

【Laravel5】X_FORWARDED_FOR の取得方法

Posted at

プロキシサーバー経由のアクセス元IPアドレスを取得するのにハマったのでメモ。

結論、SymfonyRequest#setTrustedProxies(array) を呼べばOK。
$request::setTrustedProxies([Request::ip()]);
この後に
Request::ip()
を呼ぶとアクセス元のIPアドレスを取得できます。

LaravelのRequestがSymfonyRequesを継承してて、getClientIp()メソッド(Request::ip()の参照先)コメントにちゃんと書いてありました。

* This method can read the client IP address from the "X-Forwarded-For" header
 * when trusted proxies were set via "setTrustedProxies()". The "X-Forwarded-For"
 * header value is a comma+space separated list of IP addresses, the left-most
 * being the original client, and each successive proxy that passed the request
 * adding the IP address where it received the request from.

しっかり読め、ということですねw

16
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
12