PHP Hit Counter

By Jonesy44 on Apr 20, 2008

This script doesn't take too much explaining :P
It counts an individuals clicks (respectively to their IP)

ok so;

1 Run this SQL Query in your database manger;

CREATE TABLE `ip_hits` (
`ip` VARCHAR( 255 ) NOT NULL ,
`clicks` VARCHAR( 255 ) NOT NULL ,
`date` VARCHAR( 255 ) NOT NULL

2 Place the snippet into your header/general variable page (and continue to use;


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


3 Make a new webpage called 'hits_viewer.php' and insert this code;

$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 :-)


$date = gmdate(j);
$month = gmdate(F);
$sup = gmdate(S);
$year = gmdate(Y);
$datestamp = $date. '' .$sup. ' ' .$month. ' ' .$year. '';
      :  $_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. "'");



Sign in to comment.
Hawkee   -  Apr 21, 2008

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=\"

Jonesy44   -  Apr 20, 2008

my bad, sorry, im used to having my dbconnect in the header, editing now. i find the date field to be impractial at some points. imma php newbie really, although i like to think i know everything hehe.

Do you mean assign each IP to an identification number ?

Hawkee   -  Apr 20, 2008

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.

Gemster   -  Apr 20, 2008

sorry was looking at wrong sreen, sorry for double post please delete.

Gemster   -  Apr 20, 2008

isent this in the wrong section ?
ie: php not mirc

Jonesy44   -  Apr 20, 2008

if you would like a quick example of how this can be implemented;


Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.