一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp 文本文章点击统计代码

时间:2022-07-02 23:01:02 编辑:袖梨 来源:一聚教程网

class Views {
        private $update_time = 1800;                        //更新,单位秒
        private $log_dir = './www.111com.net/log/';                //文件存放目录
        private $pid = '0';                                //文章ID
    function __construct($po_id) {
            $pid = (is_numeric($po_id) && $po_id > 0) ? intval($po_id) : 0;                //获取文章ID
            $this->pid = $pid;
    }
        //读取文档并将点击数 + 1
        function read(){
                if($this->pid > 0){
                        //$s = "";
                        $filename = SITE_ROOT.$this->log_dir.'click_log.txt';                //文件名
                        if (file_exists($filename)) {
                                $content = file_get_contents($filename);
                                if($content){
                                        $dary = explode(" ", $content);                        //把文档内容数组化
                                        foreach ($dary as $line) {
                                                $data_ary = explode('|', $line);
                                                $ary[$data_ary[0]] = $data_ary[1];
                                        }
                                }
                                if (array_key_exists($this->pid, $ary)) {                        //检查键在数组中是否存在
                                        $ary[$this->pid] = $ary[$this->pid] + 1;                //将当前的文章的点击数加1
                                } else {
                                        $ary[$this->pid] = 1;                //文章ID第一次出现那么给一个初始值1
                                }
                                foreach ($ary as $key => $val) {
                                        $s .= $key.'|'.$val." ";                //数组完成再还原成为文档格式
                                }
                                print_r($s);
                        } else {
                                $s .= $this->pid.'|1'." ";                        //这个是初始化文档www.111com.net
                        }
                }
                $fp = fopen($filename, "w");
                if(flock($fp, LOCK_EX)) {                //进行排它型锁定
                        fwrite($fp, $s);
                        flock($fp, LOCK_UN);                //释放锁定
                }
                fclose($fp);
                @chmod($filename, 0777);
        }
}
$a = new Views("3");
$a->read();

热门栏目