Posted By

cerxx on 08/13/19


Tagged

php-hacks


Versions (?)

REMOTE_ADDR shows invalid IP address on nginx


 / Published in: PHP
 

URL: http://qaru.site/questions/7176492/remoteaddr-show-wrong-ip-on-nginx

If there are no stock solutions, fixing Nginx to behave as needed would be an approach you could take. An alternative would be to fix PHP, so the desired value is available in $ SERVER ['REMOTEADDR'].

  1. function get_client_ip(){
  2. if (!empty($_SERVER['HTTP_CLIENT_IP'])) { // check ip from share internet
  3. $ip = $_SERVER['HTTP_CLIENT_IP'];
  4. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { // to check ip is pass from proxy
  5. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  6. } else {
  7. $ip = $_SERVER['REMOTE_ADDR'];
  8. }
  9. return $ip;
  10. }

Report this snippet  

You need to login to post a comment.