PHP擷取用戶端和伺服器端IP

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   使用   sp   

   

用戶端IP相關的變數
1. $_SERVER[‘REMOTE_ADDR‘]; 用戶端IP,有可能是使用者的IP,也有可能是代理的IP。

2. $_SERVER[‘HTTP_CLIENT_IP‘]; 代理端的IP,可能存在,可偽造。

3. $_SERVER[‘HTTP_X_FORWARDED_FOR‘]; 使用者是在哪個IP使用的代理,可能存在,可以偽造。

伺服器端IP相關的變數
1.$_SERVER["SERVER_NAME"],需要使用函數gethostbyname()獲得。這個變數無論在伺服器端還是用戶端均能正確顯示。

2.$_SERVER["SERVER_ADDR"],在伺服器端測試:127.0.0.1(這個與httpd.conf中BindAddress的設定值相關)。在用戶端測試結果正確。

 

類如下:

  1. class getIP{  
  2.  function clientIP(){  
  3.   $cIP = getenv(‘REMOTE_ADDR‘);  
  4.   $cIP1 = getenv(‘HTTP_X_FORWARDED_FOR‘);  
  5.   $cIP2 = getenv(‘HTTP_CLIENT_IP‘);  
  6.   $cIP1 ? $cIP = $cIP1 : null;  
  7.   $cIP2 ? $cIP = $cIP2 : null;  
  8.   return $cIP;  
  9.  }  
  10.  function serverIP(){  
  11.   return gethostbyname($_SERVER["SERVER_NAME"]);  
  12.  }  
  13. }   
  14.    
  15. $getIP  = new getIP();  
  16. $clientIp = getIP::clientIP();  
  17. $serverIp = getIP::serverIP();  
  18.    
  19. echo ‘Client IP is ‘,$clientIp,‘<br />‘;  
  20. echo ‘Server IP is ‘,$serverIp,‘<br />‘;  

PHP擷取用戶端和伺服器端IP

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.