Home > Backend Development > PHP Tutorial > PHP code to record visitor IP address

PHP code to record visitor IP address

WBOY
Release: 2016-07-25 09:07:35
Original
2929 people have browsed it
  1. //File name
  2. $filename = "ip.txt";
  3. if (isset($_SERVER['HTTP_CLIENT_IP']))
  4. {
  5. $clientip = $_SERVER['HTTP_CLIENT_IP' ];
  6. }elseif (isset($_SERVER['HTTP_X_FORWARD_FOR']))
  7. {
  8. $clientip = $_SERVER['HTTP_X_FORWARD_FOR'];
  9. }else
  10. {
  11. $clientip = $_SERVER['REMOTE_ADDR'];
  12. }
  13. //Open the file (the file does not exist and will be created automatically)
  14. if (!$fp = fopen($filename, "a+"))
  15. {
  16. echo "Cannot open file$";
  17. exit;
  18. }
  19. //Write It also determines whether there is duplicate data
  20. while(!feof($fp))
  21. {
  22. $line = fgets($fp);
  23. if($line == ($clientip."n"))
  24. {
  25. exit; //Exit if there is duplicate data;
  26. }
  27. }
  28. //Write to file
  29. if(!fwrite($fp,$clientip."n"))
  30. {
  31. echo "Cannot write to file $filename" ;
  32. exit;
  33. }
  34. //Writing to file has been completed
  35. fclose($fp);
  36. ?>
Copy code


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