Hi everyone, have a quick question on a USERS ONLINE script I am writing.
The script just tells you the number of people who have accessed your web site in the past 5 minutes. It also gives you a number of total unique visitors in the past 24 hours (NOT days, but 24 hours ago).
The users online works perfectly but the total users online during the past 24 hours does not. It never clears itself meaning the number isn't accurate. Can anyone see what the bug is to get the total users online working?
Below is the complete source code for this portion of the script. The other script involves the MySQL table creation.
A sample from my day_time column
Any assistance in getting this working properly would be very much appreciated.
The script just tells you the number of people who have accessed your web site in the past 5 minutes. It also gives you a number of total unique visitors in the past 24 hours (NOT days, but 24 hours ago).
The users online works perfectly but the total users online during the past 24 hours does not. It never clears itself meaning the number isn't accurate. Can anyone see what the bug is to get the total users online working?
Below is the complete source code for this portion of the script. The other script involves the MySQL table creation.
Code:
#!/usr/bin/perl
use warnings;
use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw/:standard/;
use CGI::Cookie;
use DBI;
####################
# CONFIG
####################
my $dbase = "spyders_usersonline";
my $mysql_user = "spyders_admin";
my $mysql_pass = "pass";
# The above three variables are your database name, username and password.
my $minutes = "5";
####################
# Do NOT edit below this line
####################
######
# Connecting to our database
######
my $dbh = DBI->connect("DBI:mysql:$dbase", $mysql_user, $mysql_pass) or
print DBI=>"errstr";
my $userip = $ENV{REMOTE_ADDR};
##########
# Selecting our database columns
#########
my $data = qq(SELECT id, ip, lasttime FROM now_time WHERE ip="$userip");
my $sth = $dbh->prepare($data);
$sth->execute() or die $dbh->errstr;
print header;
if ($sth->rows < 1) # IP is new within the past X minutes
{
my $sth = $dbh->prepare( qq{
INSERT INTO now_time (id, ip, lasttime) VALUES (?,?,?)
});
$sth->execute("id","$userip", time()) or die $dbh->errstr;
my $sth = $dbh->prepare( qq{
INSERT INTO day_time (id, ip, lasttime) VALUES (?,?,?)
});
$sth->execute("id","$userip", time()) or die $dbh->errstr;
}
else
{
my $time = time();
my $data = qq(UPDATE now_time SET lasttime=$time WHERE ip="$userip");
my $sth = $dbh->prepare($data);
$sth->execute() or die $dbh->errstr;
my $data = qq(UPDATE day_time SET lasttime=$time WHERE ip="$userip");
my $sth = $dbh->prepare($data);
$sth->execute() or die $dbh->errstr;
}
################################
# Remove expired users from now_time and day_time tables
################################
my $timenow = time();
$minutes = $minutes * 60;
my $timemin = $timenow - $minutes;
my $data = qq(DELETE FROM now_time WHERE lasttime < "$timemin");
my $sth = $dbh->prepare($data);
$sth->execute() or die $dbh->errstr;
my $day = 60 * 60 * 24;
my $timeday = $timenow - $day;
my $data = qq(DELETE FROM day_time WHERE lasttime < "$timeday");
my $sth = $dbh->prepare($data);
$sth->execute() or die $dbh->errstr;
################################
# Printout number of current users
################################
my $sql = "SELECT ip FROM now_time";
my $rows = $dbh->do($sql);
print "<b>$rows</b> users online";
my $sql = "SELECT ip FROM day_time";
my $rows = $dbh->do($sql);
print "<br><b>$rows</b> users seen today<br>";
print "<font size=\"1.5\">^ beta testing ^</font>";
A sample from my day_time column
Code:
id ip lasttime
2020 216.32.90.218 1126281068
1455 216.32.90.218 1126281068
2017 216.32.90.218 1126281068
1916 216.32.90.218 1126281068
Any assistance in getting this working properly would be very much appreciated.