How do I verify my web visitor is still online


 
Thread Tools Search this Thread
Top Forums Web Development How do I verify my web visitor is still online
# 1  
Old 01-13-2009
Data How do I verify my web visitor is still online

Hello every one,

I have a little issue that has been killing me now for the past couple of days, I have tried to find solutions online, but its been hard to, ok here it goes...

I have created a site that is based on amount of user that have access at a time, based on cookie. So if the browser is closed, I am able to remove user from my database, however, when the pc freezes or pc just shuts down, I can't tell the database that user has been removed.

I am looking for a solution via shell if possible, maybe based on the user ip, shell checks database and checks if this ip is active, and if not the user is removed from database, then I can add to cron job.

Any help, I would be gratefull thank you
# 2  
Old 01-15-2009
In your database, do you track the most-recently-used IP address for the user? That would help. Then your cron script queries the database for a logged-in users, gets the IPs, and then loops through each IP with some utility to check if the IP is still there.

The problem with the last step is what to do if that IP is behind a firewall.

In my opinion, you are better off having an "expiry" time for server-side cache objects. So every time the user interacts with the web server, a most-recently-used timestamp is updated for that user. The cron job periodically sweeps through this table, removing entries older than, say, an hour.
# 3  
Old 01-15-2009
Hi heman007,

I agree with otheus. Additionally, checking the IP of the user's machine would probably be only possible and practical on intranets. User's accessing your site from a computer shop will all have the same IP.

Like what otheus said, set your cookies "expires" property. In your table you'll have a column, say 'LastAccess', which updates everytime a user does something in your site (like clicking a post).

You must then have a script at the server-side that regularly checks the LastAccess field vs. the current time. If the difference exceeds a certain threshold (which translates to inactivity of the user) then you can delete or flag that record. Normally, the threshold will be equal to the value of the "expires" property of the cookie.
# 4  
Old 01-15-2009
I moved the thread ... I hope you all can still find it.
# 5  
Old 01-19-2009
Thank you for your post otheus, sorry this is late, just been away for a couple of days. Well what have done so far, is to use the time, and not the ip address in shell script, I check the database every 5 mins to see if the time registered is not ever a certain amount of time.

This is because the page are time are been updated, and am running a cron job every 5mins to check.

the down side to this is if the I have a lot of active user, it means that it will have to go through a lot of data and its not instant as well.

i have read of using sockets in php, does anyone have the experience for that, thank you for your help.
# 6  
Old 02-06-2009
One thing you can do with Apache is to create a seperate logfile with just the Cookie (and access time). You can then send this log through a program which monitors the last time a cookie was used, and if it has gone past the time you want, you remove it from the database. So a configuration in apache would look like:
Code:
CustomLog "|/usr/local/bin/user_tracker" cookies
LogFormat "%v\t%{s}t\t%200{Foobar}C" cookies

Where Foobar is the name of your cookie that identifies the user session.
The logs are sent to a script (/usr/local/bin/user_tracker) like this one:
Code:
#!/usr/bin/perl -n
chomp;
($vhost,$time,$cookie)=split("\t");

# Remember this cookie
$Cookies{$vhost . ":" .  $cookie}=$time;

# Every 5 minutes, scan through cookies and remove obsolete database entries
if ($time >= $next_check)  {
    while ( ($cookie,$cookie_time) = each(%Cookies) ) {
       if ($cookie_time >= $expire_time) {
         delete $Cookie{$cookie}; 
         push @to_delete, $cookie;
       }
    }
    foreach (@to_delete) { 
         ($vhost, $cookie_name, $cookie_value) = m/^(.*?):(.*?)=(.*)/;
         # Insert code here to delete $cookie_value from database
       }
    }
    $next_check = $time + 5*60;    # Check for expired cookies every 5 minutes
    $expire_time = $time + 20*60;   # Expire cookies every 20 minutes
}

Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Suggestion: visitor graphs

Perhaps we could think of visitor graphs that would give a sense of both the popularity of the forum and - more importantly - the popularity of Linux and the Open Source operating systems movement. Something similar to what sourceforge has done for their projects: SourceForge.net: Project... (2 Replies)
Discussion started by: figaro
2 Replies
Login or Register to Ask a Question