• 文件记录网页访问量


    文件记录网页访问量,高并发下使用正常;

    public function a()
    {
    //dirname() 返回目录名称;
    //__FILE__ 当前内容写在哪个文件就显示这个文件目录+文件名;
    //获取同级,目录下的tongji.db文件路径
    $file = dirname(__FILE__).'/tongji.db';
    //$data = unserialize(file_get_contents($file));
    //打开文件
    $fp=fopen($file,'ar+');
    $content='';

    //flock() 函数锁定或释放文件。
    if (flock($fp,LOCK_EX)){ //要取得独占锁定(写入的程序)
    //函数从文件指针中读取一行
    while (($buffer=fgets($fp,1024))!=false){
    $content=$content.$buffer;
    }
    $data=unserialize($content);//反序列化(将字符串变成内存变量)
    // dump($data);die();

    //设置记录键值
    $total = 'total';
    $month = date('Ym');
    $today = date('Ymd');
    $yesterday = date('Ymd',strtotime("-1 day"));
    $tongji = array();
    // 总访问增加
    $tongji[$total] = $data[$total] + 1;
    // 本月访问量增加
    $tongji[$month] = $data[$month] + 1;
    // 今日访问增加
    isset($data[$today])?$data[$today]:$data[$today]=0;
    $tongji[$today] = $data[$today] + 1;
    //保持昨天访问
    isset($data[$yesterday])?$data[$yesterday]:$data[$yesterday]=0;
    $tongji[$yesterday] = $data[$yesterday];

    //保存统计数据
    ftruncate($fp,0); // 将文件截断到给定的长度(清空)
    rewind($fp); // 倒回文件指针的位置
    fwrite($fp, serialize($tongji));//fwrite() 函数写入文件
    flock($fp,LOCK_UN); //要释放锁定(无论共享或独占)
    fclose($fp);//关闭

    //输出数据
    $total = $tongji[$total];
    $month = $tongji[$month];
    $today = $tongji[$today];
    $yesterday = $tongji[$yesterday]?$tongji[$yesterday]:0;
    echo "document.write('访总问 {$total}, 本月 {$month}, 昨日 {$yesterday}, 今日 {$today}');";

    }
    }
  • 相关阅读:
    Python sendemail txt,html,图片及附件
    python 3 requests库2个问题
    py37 unitest+html_sendmail
    python 3 unitest批量执行用例
    phthon 3 unittest模块使用
    python 之发送邮件
    ipad已停用 连接itunes怎么办
    Ubuntu 16.04系统挂载4T硬盘
    华硕RT-AC86U路由器 AP模式实现多路由器组网,扩展主路由器的无线网范围
    ubuntu 常用命令
  • 原文地址:https://www.cnblogs.com/laijinquan/p/10008370.html
Copyright © 2020-2023  润新知