webmasters.icecaves.net -  - Blog -  -  -  -  - Forums
Hello, Guest! - Register -
ArticlesHTML & CSS ScriptsJavaScript ScriptsPHP ScriptsForums Index Feb 9,  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: 734 | (Log in to rate) |

Member Comments

Page: << Previous - 1 2 3  
Bazinga!

Age: 15
Gender: Female
Karma: 0
Okay...I'm confused. What's the 'U' word? o_o Use...ful? xD


"I'm not insane, my mother had me tested!"
Glitch Reporter

Site: icecaves.net
Age: 13
Gender: Male
Karma: +3
kyra said:
Okay...I'm confused. What's the 'U' word? o_o Use...ful? xD

Yes it's Useful!


  
Leave Me Alone.

Age: 15
Gender: Female
Karma: 0
Die tomtom. That word makes me cringe more than the words 'glitch' or 'bleach'.

Kick me, kike me, Don't you black or white me


*drools*
Moonwalked.net (:
Glitch Reporter

Site: icecaves.net
Age: 13
Gender: Male
Karma: +3
There was a glitch with this article when I added it!

Can you take your MJ lyrics here. http://twitter.com/

I'm not posting on the cbox cause I don't want to see anymore lyrics.


  
Leave Me Alone.

Age: 15
Gender: Female
Karma: 0
I posted them here http://twitter.com/Soloapple
happy?
Evil cockroach.


*drools*
Moonwalked.net (:

Age: 13
Gender: Female
Karma: +2
Thomas, don't bag out MJ lyrics xP
You know you love them too!


zaphiie. the fail ninja.

Page: << Previous - 1 2 3  

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


Daily CuteVintaged.org
The IceCaves.net Topsites


| Smiley Helper | FGN-Guild.com | Gamexe.net | Tugboat | Paint-Pops.net | Guildpets | Neo Nutters | RainbowBliss | Vintaged.org | NEOHEADS | Neo-Richies | Soo-Sweet | Darkgirl's Life | Figmint | Animal Palace | The New Fearless | Neo Crave | Moo Lovett |