php gets user's real IP

WBOY
Release: 2016-07-25 08:45:14
Original
971 people have browsed it

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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!