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


i know simple question downloaded php counter script http://www.stevedawson.com/scripts/text-counter.php

which first result on google php counter scripts , worked great expected.

i tried see if messes holding refresh in browser after 255 requests overflowed 0. how fix script? think culprit filesize() gets 1 byte of data doesn't make sense since 255 3 bytes of data right? since saves in plain-text format?

why overflow? it's php shouldn't overflow automatically mutate 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 started remake script purpose want use keep track of order numbers website.

for fix think have recast $dat bigger integer type can cast in php?

also r , w suppose strings think used constants doesn't seem cause troubles afaik.

use file_get_contents , file_put_contents instead. still have consider, there hard limit counter (see php_int_max), it's 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; 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -