一个简单的 PHP 访问计数器
2018-07-20 来源:open-open
<? // start at the top of the page since we start a session session_name('mysite_hit_counter'); session_start(); // $fn = 'hits_counter.txt'; $hits = 0; // read current hits if (($hits = file_get_contents($fn)) === false) { $hits = 0; } // write one more hit if (!isset($_SESSION['page_visited_already'])) { if (($fp = @fopen($fn, 'w')) !== false) { if (flock($fp, LOCK_EX)) { $hits++; fwrite($fp, $hits, strlen($hits)); flock($fp, LOCK_UN); $_SESSION['page_visited_already'] = 1; } fclose($fp); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP Hit Counter Example Code</title> </head> <body> <p>Page content...</p> <div class="counter"> <p>This page has <span><?=$hits;?></span> hits</p> </div> </body> </html>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:C#响应系统配置项的变更
最新资讯
热门推荐