脚本计数加2而不是1

脚本计数加2而不是1

问题描述:

I have a count.php script that counts the number of refreshes on my site. The code for it is this:

if (file_exists('countlog.txt')) 
{
    $fil = fopen('countlog.txt', r);
    $dat = fread($fil, filesize('countlog.txt')); 
    echo $dat+1;
    fclose($fil);
    $fil = fopen('countlog.txt', w);
    fwrite($fil, $dat+1);
}

else
{
    $fil = fopen('countlog.txt', w);
    fwrite($fil, 1);
    echo '1';
    fclose($fil);
}

The issue is that when I try to run it, it always count's as two. I have tried to edit the: echo $dat+1 to just echo $dat but it doesn't seem to work.

Any help?

我有一个count.php脚本,用于计算我网站上的刷新次数。 代码是这样的: p>

  if(file_exists('countlog.txt'))
 {
 $ fil = fopen('countlog.txt',r)  ; 
 $ dat = fread($ fil,filesize('countlog.txt'));  
 echo $ dat + 1; 
 fclose($ fil); 
 $ fil = fopen('countlog.txt',w); 
 fwrite($ fil,$ dat + 1); 
} 
  
else 
 {
 $ fil = fopen('countlog.txt',w); 
 fwrite($ fil,1); 
 echo'1'; 
 fclose($ fil); 
} \  n  code>  pre> 
 
 

问题在于,当我尝试运行它时,它总是算作两个。 我曾尝试将 echo $ dat + 1 code>编辑为 echo $ dat code>,但它似乎不起作用。 p>

有任何帮助吗? p> div>

You have error_resporting on? I think not.

Try to:

if (file_exists('countlog.txt')) 
    {
        $fil = fopen('countlog.txt', 'r');
        $dat = fread($fil, filesize('countlog.txt')); 
        echo $dat+1;
        fclose($fil);
        $fil = fopen('countlog.txt', 'w');
        fwrite($fil, $dat+1);
    }

    else
    {
        $fil = fopen('countlog.txt', 'w');
        fwrite($fil, 1);
        echo '1';
        fclose($fil);
    }

Check this: ('countlog.txt', 'w')you missed the 'w'.

The error that outputs if you miss the single quote ' is this:

Notice: Use of undefined constant r - assumed 'r'