Jump to content

Sessions- Users


Ashoar

Recommended Posts

Had a search of the forum but couldn't really find much that related to much to my desired result.

 

Currently when a user logs in, once the system has authenticated them it will then log their details into an "online" database table like so:

 

if($row['Activated'] > 0)
{

$_SESSION['s_logged_n'] = 'true';
$_SESSION['s_username'] = $username;
$_SESSION['s_name'] = $row['Name'];   

$insertuser="INSERT INTO online(Username) values('$username')";
mysql_query($insertuser) or die("Could not login insert user");
header("Location: index.php");
} 
else 
{

 

Then in my information center i just pull all results from the "online" table to display the users online.

 

Now off course by using this method the only possible way a name can be removed from the database is if the user manually hits the logout button where i then run:

 

$username = $_SESSION['s_username'];
$query = "DELETE FROM online WHERE username = '".mysql_real_escape_string($_SESSION['s_username'])."';";
$result = mysql_query($query); 
$_SESSION['s_logged_n'] = '';
$_SESSION['s_name'] = '';
$_SESSION['s_username'] = '';

session_destroy();

 

 

If a user just closes the browser without logging out they will stay logged in for the default php session length before the session is destroyed. The problem here is that it won't remove them from the online table meaning they will always be displayed in users online. When they login next their name will then appear 2 times in users online and only get taken off if they actually click logout.

 

Now i can always add:

ini_set(’session.gc_maxlifetime’, ‘0′); 

Which would mean the session would not end unless they click log out regardless of whether they leave or not.

 

Now i am absolutely hopeless with sessions with timestamps, so does anyone have some insight as to how i could implement it into the current code and the code of each page for the timestamps?

Link to comment
Share on other sites

When they login next their name will then appear 2 times
You should make the Username column a unique key to prevent that.

 

You need to store the 'last access' date/time in the online table as well. You would UPDATE the last access date/time on every page request to keep it current. You can then check and remove records in the online table that have a last access date/time older than a value you pick (typically 10-20 minutes is used.)

Link to comment
Share on other sites

Thanks for the reply.

That is the part i know, i'm just stumped as to how i can apply it or add it to the current code i have there. As i said above i am dreadful when it comes to sessions considering their simplicity. Any chance of showing how it can be implemented there?

Link to comment
Share on other sites

It has nothing to do with sessions and in fact I did not even mention the word session in my reply.

 

There are countless 'who is online' php scripts posted around that show how to add/check/remove records from an 'online' table - http://www.google.com/#hl=en&source=hp&q=php+who+is+online+script&aq=0&aqi=g1&aql=&oq=php+who+is+online&gs_rfai=&fp=bcdf8cbbf06dc4f

Link to comment
Share on other sites

When they login next their name will then appear 2 times
You should make the Username column a unique key to prevent that.

 

You need to store the 'last access' date/time in the online table as well. You would UPDATE the last access date/time on every page request to keep it current. You can then check and remove records in the online table that have a last access date/time older than a value you pick (typically 10-20 minutes is used.)

 

You can apply this method to your users table aswell just add a last_access field and update on each request.

 

UPDATE users SET last_access = now() WHERE id = $uid

 

To select all 'logged in' users:

 

SELECT username FROM users WHERE unix_timestamp(last_access) + 300 > now()

 

A user is allowed 5 minutes to be idle before he is considered logged out

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.