The Basics
A counter is a useful tool that shows how many people have visited
the site. We've all seen them, usually at the bottom of a web page.
There are plenty of free online counters that you can embed in your
web page, but these often have downsides, such as having to create an
account or post a link to the counter company's web site. If you are
using PHP, you can create your own counter easily and efficiently and
skip the hassle.
The Script
This script is basically two simple functions. Choose a filename for
the $counterFile variable and call incrementCounter($counterFile) on
every page you want to increment the counter. Then, to display the counter,
ouput the results of the readCounter function to the screen using echo.
Simple and easy!
Script
Notes
As you examine the script you may notice that we are using two different
functions to access our file, file_get_contents() and fopen(). The reason
is that file_get_contents() performs much better that the fread() function
that is used with fopen(). So to open we'll use file_get_contents()
and to write we'll use fopen() and then fwrite(). The cousin function
of file_get_contents(), the file_put_contents() function, was not made
part of PHP until PHP5. Many web hosting companies still use PHP4, so
for compatibility, we are going to avoid using it.
Extending
this example
One problem with this script is that every time a user refreshes the
page, the counter goes up. This may not be what you want. Often, the
best statistic is the number of unique users. There are several ways
of doing this in PHP.
The classic way of recording unique users is using cookies. The idea
is to issue a cookie when the user first visits the site. Then every
time you increment the counter, check first to see if a cookie has been
set. If it has, then the user has already visited. When setting the
cookie, you can choose the length of time before the cookie expires.
You could even make the cookie expire when the user closes their browser.
This is not an exact science, because some users have set their browsers
to refuse cookies, and some clean cookies out regularly.
Another option is to use PHP's built in sessions mechanism. Each unique
user initiates a session when visiting your site and the PHP server
keeps track of current and expired sessions. So when a visitor first
visits, you set a session variable indicating that they have visited,
and then check for that variable before incrementing your counter. Depending
on how your PHP is setup, PHP may initiate a cookie for you to store
this session information. Session expiration length can be set in your
PHP configuration file.
Yet another way of recording unique visitors involves using a database.
It is pretty much the same theory as above, but instead you store user
identifiable information in the database such as a hash of the user's
IP address and user agent string, and then check for a match before
incrementing the variable. The benefit of recording users in a database
is that you can interact with your database using SQL, making it easy
to run searches based on different criteria.
I have another, more advanced object-oriented
counter example where I use cookies to keep track of return visitors.
|