Top

PHP Hit Counter

Please Register to submit score.
Average Score  0.0
Scores Submitted  0
Date Added  Apr 20, 2008
Last Updated  Apr 20, 2008

Introduction

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;
Code:
CREATE TABLE `ip_hits` (
`ip` VARCHAR( 255 ) NOT NULL ,
`clicks` VARCHAR( 255 ) NOT NULL ,
`date` VARCHAR( 255 ) NOT NULL
) TYPE = MYISAM ;


#2 Place the snippet into your header/general variable page (and continue to use;
Code:
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
Code:
include("hits.php");


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

<?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"]. '';
}
?>

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 ;-)

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

[i]Ima put a webpage counter script on next if that would be any better for any of your uses :-)


Grab the Code

Comments

  (6)  RSS
jonesy44
Comments: 644
 
PHP Snippet:  PHP Hit Counter
Posted on Apr 20, 2008 4:17 pm
if you would like a quick example of how this can be implemented;

http://gfx.myartsonline.com/hits.php

:-)
Gemster
Comments: 56
 
PHP Snippet:  PHP Hit Counter
Posted on Apr 20, 2008 4:35 pm
isent this in the wrong section ?
ie: php not mirc
:P
Gemster
Comments: 56
 
PHP Snippet:  PHP Hit Counter
Posted on Apr 20, 2008 4:37 pm
sorry was looking at wrong sreen, sorry for double post please delete.
Thanks
Hawkee
Comments: 398
 
PHP Snippet:  PHP Hit Counter
Posted on Apr 20, 2008 4:40 pm
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.
jonesy44
Comments: 644
 
PHP Snippet:  PHP Hit Counter
Posted on Apr 20, 2008 5:04 pm
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
Comments: 398
 
PHP Snippet:  PHP Hit Counter
Posted on Apr 21, 2008 3:12 am
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="

Please Register or Login to start posting comments.
Bottom