How to make own PHP counter code-script ?

Posted By: KP On June 19, 2009 Under Internet, Other, Programming, Web Design, Web Master Categories

Many website counter sites are available now a days but still if you want to make own website counter small PHP code can work for it.

Step 1: Make a notepad file and name it “hit.txt” and write initial value of your counter. For example 0

Step 2: Upload the file to your server and give it at least 664  permission.

Step 3: Put below code in your PHP pages.

<?php $fp=fopen(“hit.txt”,”r”); $hit=fread($fp,filesize(“hit.txt”)); fclose($fp); $hit=$hit+1; $fp=fopen(“hit.txt”,”w”); fwrite($fp,$hit); fclose($fp); ?>

Step 4: Put below code where you want to display the counter.

<?php echo $hit;?>

You have done it.

Please change the file name for security reasons.Your server must support fopen function.

It is not recommended to use the code on high traffic site.It’s known issue.Because the text file gets ,multiple access and it gets rewritten many times in a second.That makes the count unpredictable and invalid or wrong value.



Share with Friends ( Sharing is Caring! )


Subscribe For E-mail Notification Of Article Updates

Enter your email address:

NOTICE:
DO NOT FORGET TO CLICK ON THE VERIFICATION LINK AFTER SUBSCRIBING,THE VERIFICATION MAIL WILL BE SENT TO YOU FROM "FeedBurner Email Subscriptions".
( What is this? Why I need to do this?)

Related Posts

Post a Comment

OR