当前位置:Gxlcms > PHP教程 > 一个简单的PHP访问计数器

一个简单的PHP访问计数器

时间:2021-07-01 10:21:17 帮助过:14人阅读

<无详细内容>
  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

人气教程排行