webmasters.icecaves.net -  - Blog -  -  -  -  - Forums
Hello, Guest! - Register -
ArticlesHTML & CSS ScriptsJavaScript ScriptsPHP ScriptsForums Index Mar 11,  PST
Making a hit counter with PHP and MySQL
Written by: Thomas | This article will show you how to make a hit counter using PHP and MySQL.
  Mxx Digg StumbleUpon  

This hit counter can be used on a website. It will count unique hits. In order for this hit counter to work, you'll need to have access to MySQL. The first step is to open phpMyAdmin. Go to SQL and paste the following code in the form.

Code:

CREATE TABLE count (hits TEXT, number TEXT);
INSERT INTO count (hits, number) VALUES ('0', '1') ;
CREATE TABLE date (id TEXT, counterdate TEXT);
CREATE TABLE ip (ip TEXT, number TEXT);


Then press go. That will create the tables to run the counter. If you don't want the counter to start at 0 see 'To Reset Your Counter or to change the number of hits' below. The next step is to make a new page. I made mine count.php. Paste the following code on this page. You'll need to add your database username, password, etc. where instructions are.

Code:

<?php

mysql_connect("localhost", "USERNAME", "PASSWORD") or die(mysql_error()); //Put database username

where it says USERNAME and your database password where it says PASSWORD

mysql_select_db("DATABASE") or die(mysql_error()); //Put your database name where it says DATABASE

//You don't need to edit anything below this.


$offset = '-3';
$now = time() + (60 * 60 * $offset);



$ip = $_SERVER['REMOTE_ADDR'];

$get_date = mysql_query("SELECT counterdate FROM date");
while($data_date = mysql_fetch_array($get_date)) {
$date_current = $data_date['counterdate'];
}


$current = date("zY", $now);

if ($date_current == '') {
mysql_query("INSERT INTO date
(id, counterdate) VALUES('1', '$current')");
}


$get_date = mysql_query("SELECT counterdate FROM date");
while($data_date = mysql_fetch_array($get_date)) {
$date_current = $data_date['counterdate'];
}

if (date("zY", $now) != $date_current) {
mysql_query("DELETE FROM ip WHERE number='1'") or die(mysql_error());
mysql_query("UPDATE date SET counterdate = '$current' WHERE id='1'") or die(mysql_error());
}

$query = mysql_query("SELECT ip FROM ip WHERE ip='$ip'");
$query2 = mysql_num_rows($query);


if ($query2 == 0) {
$number = "1";
mysql_query("UPDATE count SET hits = hits + 1") or die(mysql_error());
mysql_query("INSERT INTO ip
(ip, number) VALUES('$ip', '$number')") or die(mysql_error());
}

$database = mysql_query("SELECT hits FROM count");
while($counter = mysql_fetch_array($database)) {

$total_count = $counter['hits'];

}

echo "$total_count hits";


?>

To make the counter show on your site pages

If you already know about the include() function this step should be easy for you. First you need to get the directory path to where you saved count.php. For example: /home/USERNAME/public_html/count.php all directory paths won't look like this. Once you have the path put it in the code below where is says DIRECTORY.

Code:

<?php
include('DIRECTORY');
?>


You will need to put that code on every page you want the counter to show and count hits on. Then your counter should be working!

To reset your counter or to change the number of hits

If you want to reset your counter open up phpMyAdmin and click on SQL. Paste the following code into the form. Change 'Number Here' to the number of hits you wish to change the total to.

Code:

UPDATE count SET hits='Number Here' WHERE number='1';


That's it! If you have any questions just comment below.

  Mxx Digg StumbleUpon  

| Written by: Thomas | Added: Oct 31 2009 | Last Modified: Jan 9 2010 | Views: 848 | (Log in to rate) |

Member Comments

Page: << Previous - 1 2  
ZaphiieNovember 4 2009, 11:36 pm PST - Karma: 0 - Quote - Link -
Thomas, don't bag out MJ lyrics xP
You know you love them too!

Page: << Previous - 1 2  

In order to post a comment, you must be logged into the IceCaves.net Community.
Click here to login.


Figmint WindymillHeartdrops
The IceCaves.net Topsites


Rabidish | Neoeditor | Daily Neopets | RockyRoadRules | KeliJo.net | Spicy-Sugar | Smiley Helper | Figmint | Windymill | Dash of Color | Moo Lovett | Guildpets | Arid Seas | The New Fearless | Vintaged.org | Paint-Pops.net | Neo Crave