PHP based unique visitor counter script
I was bored and in need of a small coding session, I chose a unique visitor counter since I have just set up Google Analytics on this site and wanted to see how easy it was to create a counter script.
Firstly you need to download this script and upload it to any PHP5 compatible webspace you have control over. You then just need to reference the file using RESTful queries as documented below.
How to use cntr.php
Monitoring visitors
To monitor the hits and unique visitors viewing your pages all you need to do is include the script in one of a few ways. Your options are to:
-
Reference the script as an image
To include a 1 by 1 pixel image to count visitors simply use the following line of HTML:
<img src="/cntr.php?output=pixel" alt="Visitor counter" /> -
Reference the script as a PHP include
To include the script invisibly include the following line of code on any PHP pages:
<?php include('/cntr.php'); ?> -
Reference the script as a visible image counter
To include the script as a visitor or hit style counter use the following code to show non-unique hits:
<img src="/cntr.php?output=jpeg" alt="Visitor counter" />Customize the foreground and background colours with the following parameters
bg = hex value (000000 to FFFFFF) for the background
fg = hex value (000000 to FFFFFF) for the foreground
For example:
<img src="/cntr.php?output=jpeg&bg=737373&fg=ffffff" alt="Visitor counter" />
Using the results
In order to display your visitor statistics you can use the following methods:
-
JSON
To output in the MIME type application/json use the following URI:
/cntr.php?output=json&ni=true -
XML
To output in the MIME type application/xml use the following URI:
/cntr.php?output=xml&ni=true -
JPEG Image
To display a visitor or hit style counter use the following code to show non-unique hits:
<img src="/cntr.php?output=jpeg&ni=true" alt="Visitor counter" />Customize the foreground and background colours with the following parameters
bg = hex value (000000 to FFFFFF) for the background
fg = hex value (000000 to FFFFFF) for the foreground
For example:
<img src="/cntr.php?output=jpeg&bg=737373&fg=ffffff&ni=true" alt="Visitor counter" /> -
Text
To output the details in plain text use the following URI:
/cntr.php?output=text&ni=true
RESTful Options
- output
- Output is pretty self explainatory, this is the format in which the script will return its results.
This can be set to "text", "xml", "json", "jpeg" or "pixel", if the output variable is set it defaults to "text" otherwise no output is returned. - fg
- Foreground is used to define the colour of the text used on the jpeg image.
This can be set to any hexidecimal value e.g. "ffffff", "000000", defaults to "000000". - bg
- Background is used to define the colour of the background used on the jpeg image.
This can be set to any hexidecimal value e.g. "ffffff", "000000", defaults to "ffffff". - ni
- No Increase is used to define whether the counter should be incremented or not.
This can be set to "true" or "false", defaults to "false". - ct
- Count type is used to define whether unique or overall hits are shown when output=jpeg.
This can be set to "hits" or "unique", defaults to "hits". - nt
- No text is used to define whether descriptive text is shown before the value when output=jpeg.
This can be set to "true" or "false", defaults to "false".
Distribution and reuse notice
I claim no Copyright to this script, it can be reused for whatever purpose you may have for it, feel free to link back to my site if you feel to.
