Create A System For Your Site To Monitor Hits, Unique Hits and where everyone is!
I originally wrote this tutorial for Pixel2Life forums, I have decided to rewrite it and publish it.
Lets get started!
Firstly, if you don't already have one.. create a blank file and call it
config.php
Put this in it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| <?php
$connect = mysql_connect( 'localhost', 'DATABASE USERNAME', 'DATABASE PASSWORD' ) or die(mysql_error());
mysql_select_db( 'DATABASE NAME' );
function _ip( )
{
return ( preg_match( "/^([d]{1,3}).([d]{1,3}).([d]{1,3}).([d]{1,3})$/", $_SERVER['HTTP_X_FORWARDED_FOR'] ) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'] );
}
$ip = _ip();
$time = date( 'd-m-Y' );
$timestamp = time();
?> |
In order to connect to the database, you need to fill in your database details.
Next we need to create the table..
1
2
3
4
5
6
| CREATE TABLE `stats` (
`ip` varchar(20) NOT NULL default '',
`date` varchar(10) NOT NULL default '00-00-0000',
`hits` int(10) NOT NULL default '1',
`online` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1; |
Run that in PHPmyadmin.
Next we want to have the code which updates the database with new stats, when a new visitor / hit is made.
Create a new file, call it
hit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?php
require_once 'config.php';
$getStats = mysql_query("SELECT * FROM `stats` WHERE `ip` = '" . $ip . "' && `date` = '" . $time . "'") or die(mysql_error());
if( mysql_num_rows( $getStats ) == 0 )
{
$select = mysql_query("INSERT INTO `stats` (`ip`, `date`, `hits`, `online`) VALUES ('" . $ip . "', '" . $time . "', '1', '" . $timestamp . "')") or die(mysql_error());
}
else
{
$select = mysql_query("UPDATE `stats` SET `hits` = `hits`+1, `online` = '" . $timestamp . "' WHERE `ip` = '" . $ip . "' && `date` = '" . $time . "'");
}
?> |
Instead of explaining each part, I have commented my code.
That will now either update the visitors hit count, or insert a new row for their IP.
--
To show the stats, we need to create a new file.
Create a blank file called
stats.php
Put this in it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| <?php
require_once 'config.php';
require_once 'hit.php';
$selectHits = mysql_query("SELECT * FROM `stats` WHERE `date` = '" . $time . "' GROUP BY `ip`") or die(mysql_error());
$uniqueToday = mysql_num_rows($selectHits);
$hitsToday = mysql_result(mysql_query("SELECT SUM(`hits`) as total FROM `stats` WHERE `date` = '" . $time . "' GROUP BY `date`"), 0, "total");
$totalUHits = mysql_result(mysql_query("SELECT COUNT(`hits`) FROM `stats`"), 0);
$totalHits = mysql_result(mysql_query("SELECT SUM(`hits`) as total FROM `stats`"), 0, "total");
$diff = time() - 300;
$countOnline = mysql_query("SELECT * FROM `stats` WHERE `online` > '" . $diff . "'") or die(mysql_error());
$countOnline = mysql_num_rows($countOnline);
?>
<!-- Echo stats below -->
Unique Visits Today: <?php echo $uniqueToday ?><br />
Hits Today: <?php echo $hitsToday ?><br />
<br />
Total Unique Visits: <?php echo $totalUHits ?><br />
Total Hits: <?php echo $totalHits ?><br />
<br />
Guests Online: <?php echo $countOnline ?><br />
<br /> |
Code commented, instead of explaining.
That's it! We are done!!
This has been tested, and works just fine.
A extra feature that was requested, was to see what page a user is currently viewing. I did not want to intergrate this into the main script, so you can find the new script with this feature on the next page.