How to use HTTP_X_FORWARDED_FOR properly?

前端 未结 6 1602
既然无缘
既然无缘 2020-12-05 17:01

Alright, I have an small authentication issue. My web service allows to connect to my API over HTTP with a username and password, but this connection can also be restricted

6条回答
  •  失恋的感觉
    2020-12-05 17:59

    You can use this function to get proper client IP:

    public function getClientIP(){       
         if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)){
                return  $_SERVER["HTTP_X_FORWARDED_FOR"];  
         }else if (array_key_exists('REMOTE_ADDR', $_SERVER)) { 
                return $_SERVER["REMOTE_ADDR"]; 
         }else if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) {
                return $_SERVER["HTTP_CLIENT_IP"]; 
         } 
    
         return '';
    }
    

提交回复
热议问题