PHP Question

Soldato
Joined
11 Jun 2005
Posts
3,606
Location
Liverpool
Hello, this is probabaly a simple question, I used the code posted by rob miller to make a rotating sig, and out of interest wrote a bit of code to increment a text file each time it was viewed so I could see how many times it had been viewed.

I looked a few days ago and the number was ~60,000, then I looked again today and it's now ~2,000...

The code I have is as follows:

Code:
<?php

$files = glob('{*.PNG,*.png,*.JPG,*.jpg,*.GIF,*.gif}', GLOB_BRACE);
readfile($files[array_rand($files)]);

$count_my_page = ("hits.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);

?>

I'm guessing it reset to zero when it incremented 65,535 if that would be the highest value for a 16 bit integer?

Unfortunately I don't have a great deal of experience with PHP, and was wondering if anyone can suggest a way to stop this happening?
 
I had assumed that it would have reached 1111,1111,1111,1111 (65535) and then incremented to get 1,0000,0000,0000,0000, and as it was only 16 bits just discarded the first bit, which would be why it had appeared to start again at zero (I've made mistakes like that before in other languages)

But I duplicated the files and started at 65,534 and reloaded it a few times and it worked fine, so I don't know if it was a problem with my code :confused:

I've changed it to the code you wrote anyway incase it was something that I've overlooked, as I can't think of any other reason it would reset to zero.

Thanks for your help anyway :) guess I'll just have to leave it and see if it happens again.
 
Back
Top Bottom