views:

66

answers:

2

I made a simple counter, but it increments by 2 instead of 1.

    $handle = fopen('progress.txt', 'r');
    $pro = fgets($handle);
    print $pro; // incremented by 2, WTF?
    fclose($handle);

    $handle = fopen('progress.txt', 'w');
    fwrite($handle, $pro);
    fclose($handle);

Everytime I read the file it has been incremented by 2, instead of 1.

+1  A: 
 $handle = fopen('progress.txt', 'r');
 $pro = fgets($handle);
 print $pro; // incremented by 2, WTF?
 $pro++;
 fclose($handle);

 $handle = fopen('progress.txt', 'w');
 fwrite($handle, $pro);
 fclose($handle);

That seems to work for me

kilrizzy
My bad, but the original code is like this.
Derk
My only guess could be something like Col. Shrapnel mentioned, where perhaps the page is actually being accessed twice for some reason like the site using mod-rewrite.
kilrizzy
That's it! The script was indeed run twice because of this.Thank you Col. Shrapnel and kilrizzy.
Derk
+2  A: 

Well here is the answer, based on the comment:

Be careful with front controller based on the mod_rewrite, as it act as a 404 error handler. And your browser tries to fetch favicon.ico with each request... ;)

By the way, I really love other answers. The real SO way.

Col. Shrapnel
It was actually because it was fetching a CSS file, which accessed the same script, therefore running the code twice.
Derk