The Basics
A more advanced version of our PHP
Counter, this script is object oriented and remembers return visitors
by setting a cookie. Because it remembers visitors, this counter counts
the number of unique visitors that visited your site.
The Script
This script is object oriented and written for PHP 5+, which handles
object differently than older versions of PHP. Being object oriented
allows multiple counters to exists on one page, or on different pages
throughout the site. While you can copy this class into each
page that you want to use it on, the best way is to include it in its
own file at the top of your page. For example:
Because this script uses cookies, make sure to create the Counter
object before you have any html or output! Cookies cannot be
set if anything has been already output to the browser. To create the
Counter object, call "$myCounter=new Counter()" after you
have included the script. This creates a new Counter object, increments
the counter, and sets a cookie letting the script know that the user
has visited this site.
Here is the full script:
Script
Notes
To display the value from the counter any time after you have created
the counter object use the following code:
This script is setup to be flexible as to how it stores the current
count of visitors. By default, it creates a text filed called "counter.txt"
in the same directory as the script and stores the count in there. You
can also create the Counter() object with two arguments: Counter($filename,
$folder) . This will allow you to choose the name of the file and the
folder, if any, that it is contained in. By default, the file is not
created in a folder. Here is an example:
If the folder does not exist, it will be created by the script.
Allowing these two arguments gives the developer some key functionality.
Now, instead of just one counter file for the whole site, you can have
a different counter for each page of your site. You could even have
more than one counter on a page if you wanted. You could call your counter
like the following example, and the counter file created would be the
name of each page that the counter was on:
The counter file for this page would be "myPageName.php.txt"
in this example.
|