Jump to content

Best solution for users online?


plznty

Recommended Posts

Update a userid and timestamp with every page request.

 

I made a system like this a while ago :D

 

Check it out - http://www.jackpf.co.uk. Somewhere around the middle it'll say who's online, how many strangers etc. It also updates the location of the user, so I can figure out who's viewing what thread on the forum.

 

Clever but how long until they time out, for example like how long would it take for there name 2 be removed if they like canceled internet explorer.

Link to comment
Share on other sites

well as soon as there cookie/session expires then it would remove them, I use a simple method of online 1 being yes and no being 0

when they log in it set's online to 1 in the database and when they logout sets 0 to the database thus removing them from the who is online

 

pretty simple method

 

<?php
$sql="SELECT * FROM users WHERE online = 1";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
echo "Username: ". $rows->online .";
}
?>

 

Hope this helped you

 

Link to comment
Share on other sites

jkewlo,

 

I am working on the same module. as you said once user is logged in,we can update coulmn from 0 to 1. but what for logout? i mean in my logout page only this code is there

 

unset($_COOKIE['userid']);


$update = $op->runsql("UPDATE ".tbl_author." SET status = 0 WHERE id = '$userid'");


unset($_COOKIE['lastpath']);
unset($_COOKIE['lasturi']);
session_destroy();

 

but this updation is not working for me. where you update your 1 to 0 status while logging out or on the log out page?

 

please help me.

Link to comment
Share on other sites

Logout ~

$sql1="UPDATE users SET online=0 WHERE username='". $username ."'";
  $result1 = mysql_query($sql1);

 

Login ~

$sql1="UPDATE users SET online=1 WHERE username='". $username ."'";
  $result1 = mysql_query($sql1);

Link to comment
Share on other sites

jkewlo,

 

you must have two different pages like login.php and logout.php right?

 

so on login.php my status is getting changed properly. but not on logout.php

 

do you update your both the query on same page or like me on two different pages?

Link to comment
Share on other sites

for true user online, you would need to update the db each time a page loads to the perticular user... because just because you changed "online" to 1 (for logged in) dosen't mean that if I close my web browser I will get logged out in 10-20 mins.

 

 

Link to comment
Share on other sites

Yeah, but the problem with updating a database when users log in/out is that they might not necessarily log out. They could just close their browser which would result in them being displayed as online well....forever.

 

If you follow my method you avoid that, as you can time them out. I fetch all users with a unix stamp >= time() - 600, which is 10 minutes. So if a user hasn't requested a page in the last ten minutes, they're considered offline.

 

The ajax solution is basically the same, except it will check if the user is viewing pages as well. However, this would put a lot of strain on the server. Obv the more you update the database, the slower it's going to be.

 

But yeah, that's how I did it. Works well for me.

Link to comment
Share on other sites

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.