Home > Backend Development > PHP Tutorial > A simple PHP access counter

A simple PHP access counter

WBOY
Release: 2016-07-25 09:10:44
Original
995 people have browsed it
  1. // start at the top of the page since we start a session
  2. session_name('mysite_hit_counter');
  3. session_start();
  4. //
  5. $fn = 'hits_counter.txt';
  6. $hits = 0;
  7. // read current hits
  8. if (($hits = file_get_contents($fn)) === false)
  9. {
  10. $hits = 0;
  11. }
  12. // write one more hit
  13. if (!isset($_SESSION['page_visited_already']))
  14. {
  15. if (($fp = @fopen($fn, 'w')) !== false)
  16. {
  17. if (flock($fp, LOCK_EX))
  18. {
  19. $hits++;
  20. fwrite($fp, $hits, strlen($hits));
  21. flock($fp, LOCK_UN);
  22. $_SESSION['page_visited_already'] = 1;
  23. }
  24. fclose($fp);
  25. }
  26. }
  27. ?>
  28. PHP Hit Counter Example Code
  29. Page content...

  30. This page has hits

  • 复制代码


    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