Search code examples
phprace-conditioninteger-overflowhitcounter

PHP Counter overwraps/overflows only 1 byte of data, counter resets (race condition)


I know this is a simple question but I downloaded a PHP Counter script from http://www.stevedawson.com/scripts/text-counter.php

which is the first result on google for PHP counter scripts and it worked great as expected.

I tried to see if it messes up by holding refresh in my browser after 255 requests it overflowed back to 0. How would I fix this script? I think the culprit is the filesize() which probably gets only 1 byte of data but it doesn't make sense since 255 is actually 3 bytes of data right? since it saves in plain-text format?

Why would it overflow? it's even PHP it shouldn't overflow just automatically mutate into a bigger datatype.

<?php
    $orderCountFile = "order_num_count.txt";
    if (file_exists($orderCountFile)) {
        $fil = fopen($orderCountFile, r);
        $dat = fread($fil, filesize($orderCountFile)); 
        echo $dat+1;
        fclose($fil);
        $fil = fopen($orderCountFile, w);
        fwrite($fil, $dat+1);
    } else {
        $fil = fopen($orderCountFile, w);
        fwrite($fil, 1);
        echo '1';
        fclose($fil);
    }
?>

Yeah I started to remake the script into another purpose I want to use it to keep track of order numbers for my website.

For a fix I think I have to recast $dat into a bigger integer type but can you even cast in PHP?

Also those r and w are suppose to be strings i think but they are used as constants but it doesn't seem to cause any troubles afaik.


Solution

  • Use file_get_contents and file_put_contents instead. You still have to consider, that there is a hard limit for that counter as well (see PHP_INT_MAX), but it's significantly higher.

    <?php
    $file = "counter.txt";
    $counter = 0;
    if (file_exists($file)) {
      $counter = file_get_contents($file);
    }
    
    $counter = $counter + 1;
    file_put_contents($file, $counter);
    echo $counter;