/    Sign up×
Community /Pin to ProfileBookmark

Counter resetting for some reason

I have several pages that all use the following counter code. The problem is that it seems to keep resetting back to zero (or some other very low number, I don’t really know for sure). I have set it to several large numbers like 2 million and it works fine for a while then I go back and it shows something like 243 or whatever. It does not happen right away, sometimes it is daily, some weekly, others monthly.

[code=php]$fp=fopen(“sheep_counter.txt”,”r”);

//Read the previous count
$count=fgets($fp,1024);

//close the file.
fclose($fp);

$fw=fopen(“sheep_counter.txt”,”w”);

//Increment the counter
$cnew=$count+1;

//write the counter back to the log file ie., sheep_counter.txt
$countnew=fputs($fw,$count+1);

//Display VISITOR NUMBER
echo “<br> <p align=center> You are the $cnew Visitor to This site”;
fclose($fw);[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@bokehNov 18.2006 — There are two steps in your code: read and write. The write should only be done if the read was successful so should be part of an if statement. Also you should be locking the file to prevent parallel access.
×

Success!

Help @mistux spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...