脚本计数加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?

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'