This script doesn't take too much explaining :P
It counts an individuals clicks (respectively to their IP)
ok so;
CREATE TABLE `ip_hits` (
`ip` VARCHAR( 255 ) NOT NULL ,
`clicks` VARCHAR( 255 ) NOT NULL ,
`date` VARCHAR( 255 ) NOT NULL
) TYPE = MYISAM ;
include("webpage.php");
)
or, if you do not have a page like that, make a new webpage, call it hits.php or something, and at the top of each page you want the hits to be counted, add
include("hits.php");
<?php
include("database_connect_file.php");
$sql = mysql_query("SELECT * FROM `ip_hits`");
while ($hit = mysql_fetch_array($sql)) {
echo $hit["ip"]. ' - ' .$hit["clicks"]. ' (Last visit: ' .$hit["date"]. '<br>';
}
?>
of course, you may edit that into your website's style etc. this is just some code to show an outline of how it can be done ;-)
[i]As a sidenote, if you want, as a footer or something, to show the total number of visitors, yoou can use a code something like this;
$hits = mysql_query("SELECT * FROM `ip_hits`");
$hits = mysql_num_rows($hits);
echo "Visitors: " .$hits;
note, not 100% accurate as, ofc, IPs can change from time to time
Post any bugs/errors(;-s)/comments. thanks :-)
Ima put a webpage counter script on next if that would be any better for any of your uses :-)
<?php
$date = gmdate(j);
$month = gmdate(F);
$sup = gmdate(S);
$year = gmdate(Y);
$datestamp = $date. '' .$sup. ' ' .$month. ' ' .$year. '';
$ip = ($_SERVER['HTTP_X_FORWARDED_FOR'])
? $_SERVER['HTTP_X_FORWARDED_FOR']
: $_SERVER['REMOTE_ADDR'];
$hit_sql = mysql_query("SELECT * FROM `ip_hits` WHERE `ip` = '" .$ip. "'");
if (mysql_num_rows($hit_sql) == "0") {
mysql_query("INSERT INTO `ip_hits` ( `ip` , `clicks` , `date` ) VALUES ('" .$ip. "', '1', '" .$date. "')");
}
else {
$hit = mysql_fetch_array($hit_sql);
$new_hits = $hit["clicks"] + 1;
mysql_query("UPDATE `ip_hits` SET `clicks` = '" .$new_hits. "', `date` = '" .$datestamp. "' WHERE `ip` = '" .$ip. "'");
}
?>
I mean add an auto_increment hit_id which is just an identifier for the hit. But what\'s most important is that you add an index to your ip field. You can do this through phpmyadmin by clicking the index button to the right of that field. It\'ll make sure queries fun faster when you do a \"where ip=\"
You\'re missing \"database_connect_file.php\" which you included in your hits_viewer.php. Also you should add an index for your \"ip\" field in your database since you\'re selecting on that field so much. After you get a lot of data this will become essential as it\'ll get very slow.
I also recommend including an auto_increment hit_id to your table so every one has a unique identifier. It also wouldn\'t hurt to add a timestamp field so every item will automatically be assigned a date upon modification. It\'s good to also have date, but it should be of the type \"Date\", not varchar.
if you would like a quick example of how this can be implemented;
http://gfx.myartsonline.com/hits.php
:-)