Hey everyone, this is just a simple script which will tell you how many people are online on your site and how many pages hits you’ve had in both the last 24 hours and all up.
Create database and place this dump inside it:
#
# Table structure for table `counter`
#
CREATE TABLE `counter` (
`counter` text NOT NULL
) TYPE=MyISAM;
#
# Dumping data for table `counter`
#
INSERT INTO `counter` (`counter`) VALUES ('0');
# --------------------------------------------------------
#
# Table structure for table `daily_hits`
#
CREATE TABLE `daily_hits` (
`date` text NOT NULL,
`ip` text NOT NULL
) TYPE=MyISAM;
#
# Dumping data for table `daily_hits`
#
#
# Table structure for table `usersonline`
#
CREATE TABLE `usersonline` (
`timestamp` int(15) NOT NULL default '0',
`ip` varchar(40) NOT NULL default '',
`file` varchar(100) NOT NULL default '',
PRIMARY KEY (`timestamp`),
KEY `ip` (`ip`),
KEY `file` (`file`)
) TYPE=MyISAM;
#
# Dumping data for table `usersonline`
#
INSERT INTO `usersonline` (`timestamp`, `ip`, `file`) VALUES (1035423939, '127.0.0.1', 'Mailing List');
Next you have to make the background script which will run this baby!
Create a file called: stats.php , and place this inside it.
<?php
@mysql_connect( "localhost","username","password" );
@mysql_select_db( "database_name" );
$date = date( "j/m/Y" );
$daily1 = "INSERT INTO daily_hits
(date, ip)
VALUES
('$date', '$REMOTE_ADDR') ";
$re = mysql_query( $daily1 );
$delete = "DELETE FROM daily_hits
WHERE date <> '$date' ";
$del = mysql_query( $delete );
$main_counter = mysql_query( "SELECT counter
FROM counter " );
$gy = mysql_fetch_array( $main_counter );
$new_count = $gy['counter'] + 1;
$up = mysql_query( "UPDATE counter
SET counter='$new_count'" );
$sql = mysql_query( "SELECT *
FROM users" );
$users = mysql_num_rows( $sql );
$sql = mysql_query( "SELECT *
FROM daily_hits" );
$daily = mysql_num_rows( $sql );
$sql = mysql_query("select counter from counter");
$count = mysql_fetch_array($sql);
$timeoutseconds = 30;
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;
$insert = mysql_query("INSERT INTO usersonline VALUES ( '$timestamp','$REMOTE_ADDR','$page' ) " );
if ( !( $insert ) )
{
print "Useronline Insert Failed > ";
}
$delete = mysql_query( "DELETE FROM usersonline WHERE timestamp<$timeout" );
if (!( $delete ) )
{
print "Useronline Delete Failed > ";
}
$result = mysql_query( "SELECT DISTINCT ip FROM usersonline" );
if ( !( $result ) )
{
print "Useronline Select Error > ";
}
$user = mysql_num_rows( $result );
if ( $user == '1' )
{
$online = "$user";
} else {
$online = "$user";
}
?>
Now thats the hard work done, all you have to do now is call that script onto your page:
On the page you want the counter, place this before anything else on the page (Before tag ):
<?php
include( "/path/to/stats.php" );
?>
Now, where you want the users online and site hits to be placed on your page just put this code:
<b><?=$online ?></b> user online. With: <b><?=$daily ?></b> Pages views today, and a total of: <b><?php echo $count[counter] ?></b> views.
This will be the first in a number of tutorials i’ll be putting up. Hope this helps someone.