-
-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ip()
helper to request
#523
Add ip()
helper to request
#523
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting the IP Address is tricky I think we need to take into consideration HTTP_X_FORWARDED_FOR
https://stackoverflow.com/questions/7835030/obtaining-client-ip-address-from-a-wsgi-app-using-eventlet
Hmm you're right ! Interesting ! They indeed use |
It might be good to maybe somehow take into account a user defined order of getting the IP? Thinking in the event that some Azure or AWS setup has custom proxy or something. Maybe make this a middleware? See this answer: public function getIp(){
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
if (array_key_exists($key, $_SERVER) === true){
foreach (explode(',', $_SERVER[$key]) as $ip){
$ip = trim($ip); // just to be safe
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
return $ip;
}
}
}
}
return request()->ip(); // it will return server ip when no client ip found
} https://stackoverflow.com/questions/33268683/how-to-get-client-ip-address-in-laravel-5 |
Maybe we make it a middleware like this: class IPMiddleware(Middleware):
___headers__ = [
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR'
]
#.. and you can inherit the middleware and overwrite this list if you needed a custom order lookup? |
That's smart I like it ! So the middleware would add the ip in the request ? How would you add |
umm. we can add an |
What do you think of this ? |
Looks good. Ill take a better look tonight |
Allow to retrieve client IP easily from request instance.