Home >Backend Development >PHP Tutorial >php gets user's real IP

php gets user's real IP

WBOY
WBOYOriginal
2016-07-25 08:45:141038browse

If the user uses a proxy server, the real IP is not in the HTTP_CLIENT_IP header, but needs to be parsed through the http header HTTP_X_FORWARDED_FOR.

The following php function:

  1. function GetIP() { //Get IP
  2. if ($_SERVER["HTTP_X_FORWARDED_FOR"])
  3. $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
  4. else if ( $_SERVER["HTTP_CLIENT_IP"])
  5. $ip = $_SERVER["HTTP_CLIENT_IP"];
  6. else if ($_SERVER["REMOTE_ADDR"])
  7. $ip = $_SERVER["REMOTE_ADDR"];
  8. else if (getenv( "HTTP_X_FORWARDED_FOR"))
  9. $ip = getenv("HTTP_X_FORWARDED_FOR");
  10. else if (getenv("HTTP_CLIENT_IP"))
  11. $ip = getenv("HTTP_CLIENT_IP");
  12. else if (getenv("REMOTE_ADDR"))
  13. $ip = getenv("REMOTE_ADDR");
  14. else
  15. $ip = "Unknown";
  16. return $ip;
  17. }
  18. ?>
Copy code

php


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn