$type=slider$count=4$ico=1$cate=0$rm=0$sn=0$cm=0$show=home$meta=hide

A Simple Website Counter Script using PHP

Create a simple website counter script using PHP.
Just the other day, I was asked about how to create a website counter script. Mostly the question is for PHP, so, here, instead of directing you to sophisticated scripts - especially for beginners - we'll show you how to do that in a simple and effective way starting from scratch. To solve this problem, you'll need a place to store your data - visitors no - in some place. Then when someone visit your website, just read the value form the old data and increase by 1 then display the value and replace the value in your storage place. First of all, create a file called count.dat. Open your favorite text editor and create a new PHP class for this purpose and a method to handle the data lets call this file counter.php.
So, here in this simple hitCounter class, what we have is basically a function that read the file for any old value and increment that by one then store it back. It then return the new value. Save both the count.dat and counter.php files in the same folder. Now, to use this script - say in your home page (index.php) - all you have to do is include the file and create a new instance of the class as follows:
So, now open your home page and you should see that your page increase every time you refresh it. The limitation here is that if a person view your site many times a day your site will keep increasing the value, so, you'll not get a right numbers of visitors in your website. Another limitation is that if the count.dat file is not created beforehand then this script will give an error. In the next section we'll show you how to overcome that. You can use a cookie to know if the person has visited your site before. Then also, add functionality if the data storage file exist or not? To the previous class add the following:
Here, we've added a number of hours that a person who visit the website to be considered as a new visitor. Then a cookie name is given. A cookie is a small file that is sent to the user computer and stored there. Every time a user visit the site we can access the cookie and check if it is still exist. So here, we set the time as 24 hours before our cookie expires. You can change this to any value you want. The method setHitCount() now check first if the file exist or not, if not then it create a file. Next the method check if the cookie exist or not? If cookie is not there then the function increment the value of counter and stores it in the file. As, you can see we've added a new method getHitCount() which will just retrieve the value from the file. Now, to set cookie it is always done at the beginning of a document before any HTML content is there. To do that we've created an instance of the class and call the method setHitCount();. Also, you'll need to include this file at the top of your php document. Here, is an example of how to use this class:
With this simple script I hope you can enhance it more for your needs. You can also download the script from here [ Download Counter Script]

COMMENTS

3/random/post-list
Name

blog,11,chemistry,8,css,1,feature,13,html,5,java-script,2,php-mysql,3,solved-qp,4,spectroscopy,3,technology,5,videos,3,web-design,13,
ltr
item
F Diengdoh.com: A Simple Website Counter Script using PHP
A Simple Website Counter Script using PHP
Create a simple website counter script using PHP.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh42pTQAO3E4N4aypmyIMs1SWUVpEtQ5Vrqy320vnpgK9m1HAGgVLUlL5o5Z7UN9nC2ENEpPAhdjr1pEIBUEO9xLfqRFzr6na_0Zu7wbi5y-9tMdGNx7NKlzrkRlgfKpDO_9RwNIOQX5gQ/s1600/slider12.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh42pTQAO3E4N4aypmyIMs1SWUVpEtQ5Vrqy320vnpgK9m1HAGgVLUlL5o5Z7UN9nC2ENEpPAhdjr1pEIBUEO9xLfqRFzr6na_0Zu7wbi5y-9tMdGNx7NKlzrkRlgfKpDO_9RwNIOQX5gQ/s72-c/slider12.jpg
F Diengdoh.com
https://www.fdiengdoh.com/2016/01/a-simple-website-counter-script-using-php.html
https://www.fdiengdoh.com/
https://www.fdiengdoh.com/
https://www.fdiengdoh.com/2016/01/a-simple-website-counter-script-using-php.html
true
6372396504026100368
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content