Hello,
I found a simply way to create a counter that ONLY counts unique visitors rather than counting every time index.php is hit. What this counter does is counts only the session once. If the user closes their browser and comes back it will count it again, but at least you won't get every click counted.
I have put the counter on all of my website and in the index.template.php file in my Themes directory ensures that I count all the visitors no matter which page they hit first but only count them once!
Put the following in your index.template.php file:
// My Counter
include ("/PATH-TO/simplecounter.php");
// call the counter option "1" is to not display the counter on the site itself. 0 displays it, if you want to display the counts you'll have to be tackfull on where you put the counter.
counter(1);
Here's the code for simplecounter.php, I didn't bother using the db_query commands, I wrote it so it doesn't spit out errors if there is a problem with the database, because it's not critical:
<?php
@session_start();
include 'dbconnect.php';
function showimage($count) {
global $CONFIG;
$marker = 0;
while ($marker < strlen($count)) {
$digit = substr($count, $marker, 1);
$theme_dir = "/counter/theme";
$theme = 4;
$width = 10;
$height = 12;
echo '<img src="' . $theme_dir . '/' . $theme . '/' . $digit . '.jpg" width="' . $width . '" height="' . $he
ight . '" border="0">';
$marker++;
}
}
function counter($display) {
if (!session_is_registered("counted")){
mysql_query("UPDATE simplecount SET count=(count + 1) WHERE count_id=1");
session_register("counted");
}
$result = mysql_query("SELECT count from simplecount where count_id=1");
$myresult= mysql_fetch_row($result);
$count = $myresult[0];
if (strlen($count) < 6) {
$size = 7 - strlen($count);
for ($i = 1; $i < $size; $i++)
$count = 0 . $count;
}
if ($display == 0)
showimage($count);
}
?>
Here's the dbconnect.php code:
<?php
$db_user = "DB_USER";
$db_database = "localhost";
$dbname = "DBNAME";
$passwd ="YOURPASSWORD";
// Connect to Database
$db_connection = mysql_connect($db_database, $db_user, $passwd);
mysql_select_db($dbname, $db_connection);
?>
Now create a table with phpmyadmin called 'simplecount'
Add count as varchar 255
and count_id as varchar 25
Add some values to them, say count = 0 and count_id = 1
count_id is your counter ID number, you can multiple for multiple instances in the same database.
You can change "showimage" to display some text instead. I used a bunch of images called 1.jpg 2.jpg 3.jpg etc...
Hope that helps for anyone looking for a counter to count unique visitors.
can u show me a sample at least?
Sample of what? The counter? ???
You can check it out at the bottom of my main page www.cartalkcanada.com My forums are currently disabled as they are not launched yet. But if you click around on my site it's all php you will see the counter doesn't change.
Hi. I think this is what I am looking for. Has it been made into a mod package yet as I have no clue when it comes to creating databases etc. What I want is a counter that will tell me how many unique individuals have accessed the site within the last day/month and not just page hits, perhaps by comparing their IP address and possible show the info on the stats page.
The counter script in the site doesn't work for me. I clicked in various parts of the site and the counter kept increasing.