PHP点击计数器文本文件值经常重置?

时间:2013-11-21 12:09:34

标签: php counter

我在我的网站上使用这个简单的点击计数器脚本。但它的值会重置为零或频繁更改为较低的值(我没有注意到特定的时间间隔)。脚本中有任何问题吗?请帮忙 ? file path

<?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;
    }
    ?>

1 个答案:

答案 0 :(得分:0)

就我的假设而言,当用户同时访问导致文件的错误I / O时会发生此重置,从而重置它。

<强>解决方案:

  • 创建txt文件的及时备份并比较值以发布具有更高计数器的文件中的值。
  • 切换到数据库!