Mar 17,  PST

Keeping Track of your Site's Online Visitors
Written by: Thomas | This article will show you how to put an online visitor counter on your site.
  Mxx Digg StumbleUpon  

Creating the Database
The first step to making this online visit counter is to create the database. Open up phpMyAdmin and click on SQL. The code below is what you'll need to copy and paste into the form in SQL and click Go.

Code:
CREATE TABLE `views` (`ip` VARCHAR(30) NOT NULL, `time` VARCHAR(10) NOT NULL, PRIMARY KEY (`ip`));


The PHP code
Now you'll need to make a new file. It doesn't matter what you name it. Mine is called hits.php. Copy and paste the code below into the file.

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

//Do not edit anything below!

$time = time();
$ip = $_SERVER["REMOTE_ADDR"];

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

if ($query2 == 0) {
mysql_query("INSERT INTO views (ip, time) VALUES('$ip', '$time')") or die(mysql_error());
}
else
{
mysql_query("UPDATE views SET time = $time WHERE ip='$ip'") or die(mysql_error());
}

$total = $time - 120;
$delete = $time - 1800;

mysql_query("DELETE FROM views WHERE time < '$delete'") or die(mysql_error()); //Deletes records that are no longer needed

$database = mysql_query("SELECT COUNT(time) FROM views WHERE time > '$total'") or die(mysql_error());
while($views = mysql_fetch_array($database)) { //Selects and counts the visitors within the last 2 minutes

$total_views = $views['COUNT(time)'];

}

echo "Total People Viewing Site: $total_views"; //You can edit this if you want.
?>


You'll need to change the database username and password to the one you made.

Adding the visit counter to your site pages
I'll be using the include() function to make the visit counter show on your site pages. First you'll need to get the full directory path to the file where you saved the main code. It should look something like this: /home/USERNAME/public_html/hits.php. Once you get that you'll need to replace DIRECTORY in the code below with your path.

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


Put that code on every page.

If you have any questions, comment below.

  Mxx Digg StumbleUpon  

| Written by: Thomas | Added: Dec 11 2009 | Last Modified: Dec 21 2009 | Views: 959 | (Log in to rate) |

Member Comments

ZaphiieDecember 11 2009, 5:12 pm PST - Karma: 0 - Quote - Link -
4.5/5
Awesome ;D *ARTICLE! ARTICLE!*
Puffs of ipawd.netDecember 11 2009, 5:12 pm PST - Karma: 0 - Quote - Link -
4.5/5
Awesome! And surprisingly it was written by Thomas...
Grace of moonwalked.netDecember 12 2009, 2:44 pm PST - Karma: 0 - Quote - Link -
5/5
Wow (: Great script!

Puffs said:
Awesome! And surprisingly it was written by Thomas...

Exactly what I was thinking...
Aashni of t-n-f.co.nrJanuary 4 2010, 3:35 am PST - Karma: 0 - Quote - Link -
Nice article, though one question... could you add a bit onto the tutorial which shows exactly what will come on the screen where this code is added...

What will appear?
Thomas of icecaves.netJanuary 4 2010, 7:59 am PST - Karma: 0 - Quote - Link -
  
Code:
echo "Total People Viewing Site: $total_views"; //You can edit this if you want.

It will say "Total People Viewing Site: Number Here". You can always change that if you like.
Aashni of t-n-f.co.nrJanuary 4 2010, 8:15 am PST - Karma: 0 - Quote - Link -
[quote='Thomas']  
Code:
echo "Total People Viewing Site: $total_views"; //You can edit this if you want.

It will say "Total People Viewing Site: Number Here". You can always change that if you like. [/quote]

Merci Lol. Eventually, I'll add this onto my site
Thomas of icecaves.netJanuary 4 2010, 8:19 am PST - Karma: 0 - Quote - Link -
No problem!
Woah quotes don't hold code boxes in them! I wonder if there's a reason behind that.
Aashni of t-n-f.co.nrJanuary 4 2010, 8:25 am PST - Karma: 0 - Quote - Link -
Thomas said:

Woah quotes don't hold code boxes in them! I wonder if there's a reason behind that.


it might be because they block the code?
I bet if we ask Haily, he'll know!
Grace of moonwalked.netJanuary 4 2010, 4:51 pm PST - Karma: 0 - Quote - Link -
Haily the genius will have all the answers.  
EdgarJanuary 4 2010, 5:36 pm PST - Karma: 0 - Quote - Link -
Another glitch to report?
Aashni of t-n-f.co.nrJanuary 4 2010, 11:53 pm PST - Karma: 0 - Quote - Link -
Probably, has someone reported it yet?
Haily of icecaves.netJanuary 6 2010, 5:02 pm PST - Karma: 0 - Quote - Link -
I can fix it, but it would make it too complicated of a script. If you don't get what I mean, here's why. xD

Well, anything inside code tags is processed differently than regular posts. The code is actually sliced out of the post and processed using different code than how the regular post is processed. That's why bbcode and smilies don't work inside the code tags, and why bbcode from different slices of the post don't connect. :K I suppose I can connect the slices of the regular post, process it, disconnect it again and place back in the code slices, but it would unnecessarily make it more complicated than it already is, which will just make the script run longer for something that's not so important.
Puffs of ipawd.netJanuary 6 2010, 5:34 pm PST - Karma: 0 - Quote - Link -
Must you provide an explanation for everything, Haily?
Haily of icecaves.netJanuary 6 2010, 6:12 pm PST - Karma: 0 - Quote - Link -
Probably not. Would you want me to explain why?
Puffs of ipawd.netJanuary 6 2010, 7:02 pm PST - Karma: 0 - Quote - Link -
No thank you.
EdgarJanuary 7 2010, 6:38 am PST - Karma: 0 - Quote - Link -
Yes, yes! DO!
Not. Please, I was kidding.
Grace of moonwalked.netJanuary 8 2010, 12:09 am PST - Karma: 0 - Quote - Link -
Gah, Haily is just too clever.

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


Username:

Password:


Figmint WindymillAmour Chaleur
The IceCaves.net Topsites


Windymill | Neoeditor | Rabidish | KeliJo.net | NeopetsGuide | Hidden Star | The Neopian Bay | Skylish | Tugboat | Dash of Color | Neo Nutters | Smiley Helper | Spicy-Sugar | Vintaged.org | MSPT | Darkgirl's Life | Gamexe.net