Page 1 of 1

Users online with sessions?

Posted: Wed Jul 31, 2002 5:59 am
by Toneboy
I'm a big fan of the old Users Online script (which I initially got from Evil Walrus, but which I've seen elsewhere). However as I've seen some users visit the site and their I.P. address has changed from page to page surely a Users Online script with sessions would be both more accurate and also provide more information?

So here is my suggestion, starting with the MySql set-up:

Code: Select all

CREATE TABLE useronline ( 
pageview int(15) DEFAULT '0' NOT NULL auto_increment,
sessionid varchar(100) NOT NULL,
count int(15) DEFAULT '0' NOT NULL,
timestamp int(15) DEFAULT '0' NOT NULL, 
ip varchar(40) NOT NULL, 
file varchar(100) NOT NULL, 
PRIMARY KEY (pageview), 
KEY ip (ip), 
KEY file (file) 
);
New fields here are pageview (so it can work like a counter), sessionid (self-explanatory) and count (so you can see how many pages a visitor has seen while they've been on the site, surely a good way to see how long people stick around for?).

The script should work like this (forgive me if I've not got the cookie part right, I'm just beginning to learn them... er, much like sessions really):

Code: Select all

<?php 

// seed with microseconds
function make_seed() &#123;
list($usec, $sec) = explode(&#8217; &#8217;, microtime());
return (float) $sec + ((float) $usec * 100000);
&#125;
srand(make_seed());
$randval = rand(1, 99999);

// delete following line after testing
echo $randval;

session_start();

session_register('sessionid', 'sessioncount');

$sessionid = time();
$sessionid = $sessionid + $randval

$sessioncount&#1111;count]++;

setcookie ("Sitevisitor", $sessionid, time()+14400, "/", ".domain.com");


$server = "xxx"; 
$db_user = "xxx"; 
$db_pass = "xxx"; 
$database = "xxx"; 

$db = mysql_connect($server, $db_user,$db_pass); 
mysql_select_db($database,$db); 

$timeoutseconds = 300; //5 minutes 

$timestamp = time(); 
$timeout = $timestamp-$timeoutseconds; 

$insert = mysql_query("INSERT INTO useronline VALUES ('0','$sessionid','$sessioncount','$timestamp','$REMOTE_ADDR','$PHP_SELF')",$db); 
if(!($insert)) &#123; 
print "Useronline Insert Failed > "; 
&#125; 
$delete = mysql_query("DELETE FROM useronline WHERE timestamp<$timeout",$db); 
if(!($delete)) &#123; 
print "Useronline Delete Failed > "; 
&#125; 
$result = mysql_query("SELECT DISTINCT sessionid FROM useronline",$db); 
if(!($result)) &#123; 
print "Useronline Select Error > "; 
&#125; 
$user = mysql_num_rows($result); 


mysql_close(); 
if($user == 1) &#123; 
print("$user user online\n"); 
&#125; else &#123; 
print("$user users online\n"); 
&#125; 
?>
Am I on the right track?

Posted: Wed Jul 31, 2002 9:43 am
by fatalcure
uhm, i dont think IPs change from page to page, even if its on a 56k connection, the IP for that online time would stay the same.

And I hope you realize that 5 minutes is a LONG time for it to be stored in a database, as a new record is stored in the DB everytime the page is refreshed, or a new page is visited, by EVERY user currently browsing your site.

Posted: Wed Jul 31, 2002 10:02 am
by Toneboy
I'll tell you how I discovered what was happening.

I've got a little add-on to the users online script which recognises if a new high total of users is reached. On the day it reached six (my marketing consultancy fee is charged by the magic bean) I happened to be online, and took a look at my MySql database to look at the details. Unfortunately there were only six files there, all a natural progression from one another (i.e. main index to writers index, writers index to individual writer, etc.), with the IP address only changing on the final number of the fourth section on each occasion.

A very, very minor flaw, but one I would like to fix. 8)

Posted: Thu Aug 01, 2002 8:15 am
by mikeq
An IP address doesn't change if it is in use by someone.

How can you be sure that someone hasn't bookmarked/typed in one of your URL's without going through the natural progression of pages, this would give the result you have with a different IP opn later pages.

Posted: Thu Aug 01, 2002 9:10 am
by Toneboy
Well I'm pretty sure no-one bookmarked/typed the urls as the timestamps were all within a few seconds of one another. My logic led me to believe it was just the one user, but I don't have the expertise of most people on here so I'm happy to go with what you guys say.

Anyway, back to the subject in hand... sessions are potentially a better way to go though, wouldn't you think?

Posted: Thu Aug 01, 2002 2:56 pm
by hob_goblin