Jump to content

x users online


doddsey_65

Recommended Posts

I have a login system setup and i made it echo number of users online by inserting the username into a dif tble when they log in and echoing the numrows in tht table. And when they logout it is removed, however when the session times out and they are logged out automatically it doesnt delete the row so it still says there is a user online. Here is the logout code:

 

<?php 


$username = $_COOKIE['ID_my_site'];
$db=mysql_connect("myinfo") or die(mysql_error()); 
mysql_select_db("mydb") or die(mysql_error());


$past = time() - 100; 
setcookie(ID_my_site, gone, $past); 
setcookie(Key_my_site, gone, $past); 
$sql="DELETE FROM online WHERE username = '$username'";
header("Location: index.php"); 



?>

Link to comment
Share on other sites

User's online is often a hard thing to do. If you have access to CRON, you can create a php cron job and add a new column to your online table that says "last_activity" if that last activity is past x minutes then delete them from the table. I believe that is the only sure fire way to do it, as any useronline script you will find online does the same thing.

 

You just have to remember to update the online table each page call the users make to keep them showing as active.

Link to comment
Share on other sites

so if there is no way to delete the record when they are logged out automatically how would i change this code so they arent logged out automatically, and would that even be feasable(spelling?) ie security?

 

You can change how long the session cookie is stored on the users computer through php.ini I believe. You extend that to infinite and yea, but that still does not account for the user not clicking the logout button. The problem here lies with the user, they do not have to click "logout", they can simple browse away or close the browser. So in order for this to work you have to do the leg work and make an inactive timeout essentially.

 

Unfortunately you cannot force a user to logout without an automated job set to run at intervals to continually check if the user has been active or not for a period of time.

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.