Jump to content

Recommended Posts

Greetings!

 

I recently started learning the in's and out's of the CakePHP framework and to test my ability I began writing this little forum-style website. If you like, you can check it out here: http://tomcant.co.uk/thebored

 

I don't often run into problems and when I do I usually sort them out without too much hassle. However, I'v become quite stuck. Here's my issue:

 

When a user logs in I update the `users' table in my database to show that the user is online. When the user clicks logout I update the table again to show that the user is no longer online. I have a Who's Online section that queries the database and displays all the names of the logged in users. Simple. However, if the user simply closes their browser/tab, obviously my little databse-updating-code doesn't run and the changes aren't reflected in the Who's Online section.

 

I'v really rooted around for a solution but can't find anything suitable. The solution I'v come up with goes something like this: each time a logged in user refreshes his/her browser, update a database table to show when this activity occurred (a timestamp). Then have some script execute every 5 minutes or so checking which users have been active recently, setting the status of a user to offline if they have been idle.

 

This works to a certain extent, but what if the user was just idle and didn't click logout? My script would mark them as offline and this isn't what we want.

 

Please help, it would be most appreciated.

 

Thanks.

you'd have a PHP page with a script like

//security measures up here? i.e. if (isset($_SESSION['logged_in'])) { raghh }

$user_id = $_SESSION['id'];
$sql = @mysql_query("UPDATE users SET timestamp = UNIX_TIMESTAMP() WHERE userid = $user_id");

 

then i'd have a jquery script in the <head> of each page like so

<script>
jQuery(document).ready(function() {
function pageLoad() {
	//ajax for automatic pageload, change the .php page to whatever you make
	$.post("yourNewPhpPage.php");

	//call 30sec timeout
	setTimeout(pageLoad(), 30000);
};

pageLoad();
});
</script>

 

jquery isn't my specialty, but that *should* work...

 

 

**EDIT**

to run this you'll need to have the jquery file linked to your site...

jquery.com

I see. Could you just clear one thing up for me... what will be at `yourNewPhpPage.php' ?

 

Thanks for your help.

 

 

[EDIT]: Ok, I figured it out. Thanks again for your help. I'll post here if I have any further problems.

yourNewPhpPage.php will be a simple php page with this code, you'll have to set up the $user_id variable so it gets the userid from the $_SESSION correctly... if you have it stored in there?


//security measures up here? i.e. if (isset($_SESSION['logged_in'])) { raghh }

$user_id = $_SESSION['id'];
$sql = @mysql_query("UPDATE users SET timestamp = UNIX_TIMESTAMP() WHERE userid = $user_id");

Ok, this isn't working so clearly in my head anymore. Think of it like this: a user logs in and the database is updated with a new timestamp. The user then closes the browser. How is my site going to update the database to show that the user logged out?

the ajax keeps updating the db every 30 seconds, so if the user has logged out the timestamp will be older than 30 seconds.

and on top of that i'd also put a bit in my logout script saying

$sql = @mysql_query("UPDATE users SET loggedIn = 0 WHERE userID = $userID");

 

and then for the script to see if users are logged in

$userID = whatever;
$sql = @mysql_query("SELECT loggedIn, timestamp FROM users WHERE userID = $userID");

$userDetails = mysql_fetch_array($sql);

if ($userDetails['loggedIn'] == 1 && ((time() - $userdetails['timestamp']) < 30)) 
{
//user is logged in, echo logged in image or whatever you want to do
} else {
//user is logged off
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.