教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php 统计页面浏览次数(文本缓存)的代码

php 统计页面浏览次数(文本缓存)的代码

发布时间:2015-09-18   编辑:jiaochengji.com
很多时候,需要统计页面浏览次数,即统计多少人访问了某页面,这个代码用文本作为缓存,实现php 浏览次数的统计,适合初学者参考。

统计某页访问次数的php代码,注意学习下文本缓存的用法。
 

复制代码 代码示例:
<?php
/**
@统计页面浏览次数 文本缓存
@site http://www.jiaochengji.com
*
*/
private function visit($id)
{
    if (isset($GLOBALS['cfg_safe']['visit-article']) && $GLOBALS['cfg_safe']['visit-article'])
    {
        $file = SYS_PATH . 'cache/visit-article.txt';
       
        if (!file_exists($file))
        {
            file_put_contents($file, ',' . $id);   
        }
        else if ((time() - filectime($file)) < $GLOBALS['cfg_safe']['visit-article'])
        {
            file_put_contents($file, ',' . $id, FILE_APPEND);     
        }
        else
        {
            $string = file_get_contents($file);
            if ($string != '')
            {
                $temp = explode(',', $string);
                foreach ($temp as $row)
                {
                    if (empty($row))
                        continue;
                    $this->mysql->update('UPDATE `pcb_article` SET `visit` = `visit` + 1 WHERE `id` = ' . $row . ' LIMIT 1');
                }   
            }
            unlink($file);
        }
    }   
}

您可能感兴趣的文章:
php 统计字数(支持中英文)的实现代码
php 统计页面浏览次数(文本缓存)的代码
php 网站流量统计的原理
img标签添加php文件记录页面访问数据
你了解浏览器的刷新原理吗
CodeIgniter框架中启用和清除缓存的教程
解析 PHP和浏览器缓存机制
php统计静态html页面浏览访问次数
php输出缓存ob系列函数用法
ob_start 生成html页面代码

[关闭]
~ ~