首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP经常重设计数器文本文件值?

PHP经常重设计数器文本文件值?
EN

Stack Overflow用户
提问于 2013-11-21 12:09:34
回答 1查看 762关注 0票数 1

我正在使用这个简单的点击计数器脚本在我的网站。但是它的值重置为零,或者频繁地更改为较低的值(没有我注意到的特定时间间隔)。剧本里有什么问题吗。请帮帮忙?

代码语言:javascript
复制
<?php

    // SETUP YOUR COUNTER
    // Detailed information found in the readme.htm file

    // Count UNIQUE visitors ONLY? 1 = YES, 0 = NO
    $count_unique = 1;

    // Number of hours a visitor is considered as "unique"
    $unique_hours = 1;

    // Minimum number of digits shown (zero-padding). Set to 0 to disable.
    $min_digits = 5;

    #############################
    #     DO NOT EDIT BELOW     #
    #############################

    /* Turn error notices off */
    error_reporting(E_ALL ^ E_NOTICE);

    /* Get page and log file names */
    $page = input($_GET['page']) or die('ERROR: Missing page ID');
    $logfile = 'logs/' . $page . '.txt';

    /* Does the log exist? */
    if (file_exists($logfile))
    {
        /* Get current count */
        $count = intval(trim(file_get_contents($logfile))) or $count = 0;
        $cname = 'tcount_unique_'.$page;

        if ($count_unique==0 || !isset($_COOKIE[$cname]))
        {
            /* Increase the count by 1 */
            $count = $count + 1;
            $fp = @fopen($logfile,'w+') or die('ERROR: Can\'t write to the log file ('.$logfile.'), please make sure this file exists and is CHMOD to 666 (rw-rw-rw-)!');
            flock($fp, LOCK_EX);
            fputs($fp, $count);
            flock($fp, LOCK_UN);
            fclose($fp);

            /* Print the Cookie and P3P compact privacy policy */
            header('P3P: CP="NOI NID"');
            setcookie($cname, 1, time()+60*60*$unique_hours);
        }

        /* Is zero-padding enabled? */
        if ($min_digits > 0)
        {
            $count = sprintf('%0'.$min_digits.'s',$count);
        }

        /* Print out Javascript code and exit */
        echo 'document.write(\''.$count.'\');';
        exit();
    }
    else
    {
        die('ERROR: Invalid log file!');
    }

    /* This functin handles input parameters making sure nothing dangerous is passed in */
    function input($in)
    {
        $out = htmlentities(stripslashes($in));
        $out = str_replace(array('/','\\'), '', $out);
        return $out;
    }
    ?>
EN

回答 1

Stack Overflow用户

发布于 2014-04-28 16:20:35

据我的假设,这种重置发生在用户同时进行访问时,导致文件的I/O错误,从而重置它。

解决方案:

  • 创建txt文件的及时备份并比较值,以便从计数器较高的文件中发布值。
  • 切换到数据库!
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20120646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档