
TNX?
Buy Genuine Rolex watches onle from blowers-jewellers.co.uk visit rightpricefurniture .co.uk
|
| PHP/MySQL Tutorial |
| |
| Simple Hit Counter Script Part 1 |
| Written by : Sean |
| Date : 2003-07-29 |
[Back]
[Home] |
|
|
|
|
|
id
This is a very short & simple "Hit Counter" tutorial that will help you to create your own short & customised hit counter script using php/mysql.
1) Creating the necessary tables
There will be 2 tables that we need to create a) A table to store hit count b) A table to store visitor's information
Use phpmyadmin or an database tool to create the 2 tables using the SQL queries below :-
| Code: |
CREATE TABLE hit_counter( id int(10) unsigned PRIMARY KEY 0 , hits int(10) unsigned 0 , ); INSERT INTO hit_counter (id,hits) VALUES('1','0');
CREATE TABLE log( id int(10) unsigned PRIMARY KEY auto_increment , ip char(15) , host char(100) , referrer char(100) page char(100) , timestamp int(10) 0 , ); |
Now we are ready to write ourselves a hit counter script.
2) The Script a) Defining the variables First we are going to define the necessary parameter for this script. This will make our script more structured.
| Code: |
<? // Define the log table name as per above SQL Statement // define("LOG_TBL", "log"); // Define the Hit Counter table name as per above SQL Statement // define("HIT_COUNTER_TBL", "hit_counter"); // // Define IP address u need to block // define("BLOCK_IP", "0.0.0.0"); // Define unique time you want to count as a hit after the last visit (in minutes) // define("UNIQUE_TIME", "1440"); // Define duration of thethe Visitor's counted as Currently online // define("USER_ONLINE_TIME", "5"); |
b) Collecting Visitor's information
We have gone thru this portion before
| Code: |
// get the visting time $u_timestamp = time(); //capture IP address if (@getenv("HTTP_X_FORWARDED_FOR")) { $u_ip = @getenv("HTTP_X_FORWARDED_FOR"); } else { $u_ip = @getenv("REMOTE_ADDR"); } // If block ip exit if ($u_ip == BLOCK_IP) { return 1; exit; } // get host name $u_host = @gethostbyaddr($u_ip); // get the refer site $u_referrer = @getenv("HTTP_REFERER"); |
c) Connecting to Database
You need to have a section of your script to connect to mysql database Fill-up the necessary date to connect to your mysql database .. eg: Host name, user login name, database name & passwords
| Code: |
$host = "localhost"; $login = ""; $dbase = ""; $password = ""; $db = mysql_connect($host,$login,$password); mysql_select_db($dbase,$db); |
d) Managing Hit counter information
After we get ourselves connected to the database, Now it's time for us to check, update information into the database
Start of with checking whether the visitor has been here before ( last 24 hour ). Following is the SQL statment which allows you to do this.
| Code: |
$sql= "SELECT COUNT(*)as row FROM " . LOG_TBL . " WHERE (ip LIKE \"%" . $u_ip . "%\") AND (timestamp + " . (UNIQUE_TIME * 60) . " > " . $u_timestamp . ")"; $query = mysql_query($sql) or die("Line 31 Cannot query the database.<br>" . mysql_error()); $numrows = mysql_fetch_array($query); |
If the ip not found that count as a hit and add on to the current hit counter
| Code: |
If ($numrows['row'] <= 0) { //sql statement to get current hit count $sql = "SELECT hits FROM " . HIT_COUNTER_TBL . " WHERE (id=1) LIMIT 1"; $query = mysql_query($sql) or die("Line 35 Cannot query the database.<br>" . mysql_error()); $row = mysql_fetch_array($query); $hits = $row["hits"]; |
Add 1 to the current hit count & update the database
| Code: |
$sql = "UPDATE " . HIT_COUNTER_TBL . " SET hits=(hits+1) WHERE id=1" ; mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error()); |
That's all for part 1 .. may sure u be back for part 2 |
|
|
|