Members On Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
daniworldwide
Forum Newbie
Posts: 14
Joined: Mon May 26, 2003 12:30 pm
Location: Colorado

Members On Script

Post by daniworldwide »

Is there any way to keep track of when a person clicks out of the current page?

Example: If a member clicks a page on my site, it will show up that he is currently on that page, and when he loads a new page, it doesn't show up anymore?

I have something like it for my site currently. Members who are currently logged on. Its a simple time function. If you logged in, in the last hour, you are "online." I'd like it more exact, but I don't quite know how to do it. This is my current function, roughly.

Log.php:

Code: Select all

<?php


$timenow = time();

$sql = ("UPDATE members SET atime='$timenow' WHERE mid='$mid'")
or die ("cannot select updae active time");

$result = mysql_query($sql);

$sql = ("UPDATE members SET logged_on='1' WHERE mid='$mid'")
or die ("cannot select updae active time");

$result = mysql_query($sql);



?>
Logoff.php

Code: Select all

<?php


$timenow = time();


$result = mysql_query("SELECT mid, atime FROM members WHERE logged_on='1'")
or die ("cannot select last logged in time");

while ($row = mysql_fetch_array($result))
{
$mid = $row["mid"];
$atime = $row["atime"];

$timediff = $timenow - $atime;

 if ( $timediff >= 3600) 
{

$sql = mysql_query("UPDATE members SET logged_on='0' where mid='$mid'") or die ('cannot log off membrers');

$sql = mysql_query("UPDATE members SET atime='0' where mid='$mid'") or die ('cannot reset members last active time on the site');

} //end if
} //end while


?>
Thanks!

-Dani
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

...

Post by kettle_drum »

Umm to make it more accurate you could make it so that users were offline if they havent requested a page in like 5 mins.

Something else you could try is using the javascipt function onUnload to open a new window that would tell the server that the user was leaving the site. But remember that popups are HIGHLY annoying. Maybe you could try this same method but using a frame with 0 width/height.
daniworldwide
Forum Newbie
Posts: 14
Joined: Mon May 26, 2003 12:30 pm
Location: Colorado

Post by daniworldwide »

Thats a very good idea, Thanks!
Post Reply