Jump to content


Photo

sessions table


  • Please log in to reply
8 replies to this topic

#1 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 09 August 2006 - 11:41 AM

if a user logs in to a website, is there any way of entering the session name into a table, then when they log out it automatically deletes the session record? so that only active logins are in the table?

i obviously know how to enter it into the table but i dont know how to automatically delete it.

also, is there anyway of doing it so that when the user closes the browser it will delete the entry because most sessions auto delete when the browser is closed?

thanks

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 09 August 2006 - 11:52 AM

You could have a field in your session table that's called last_click and every time they go to another page it will be updated like this:
mysql_query("UPDATE sessions SET last_click='".time()."' WHERE id='{$_SESSION['id']}' LIMIT 1");
Then you could check like this if they have been inactive for a long time:
$query = mysql_query("SELECT * FROM sessions WHERE id='{$_SESSION['id']}'");
$data = mysql_fetch_assoc($query);
if($data['last_click']+(60*30) < time())
{
	mysql_query("DELETE FROM sessions WHERE id='{$_SESSION['id']}'");
	unset($_SESSION['id']);
}
else {
	// they are still logged in
}


#3 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 09 August 2006 - 11:53 AM

I dont think that is possible.

I am guessing your are looking for a script which tells who is logged in

What you can do is

On every page load, check if a user is logged in, if they are update the MySQL table to say the date and time

then to check who is logged in, check everyone who has action within 10 minutes.

Tell me the problem, I will try tell you the solution

#4 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 09 August 2006 - 12:03 PM

if the user has closed the bowser the session would be deleted and if they try to log in again then they would have to log in again anyway. but what i want is an active users list. but if i used the last_click method then the users record would not be deleted if the browser was closed. so how would i do it so that the record is deleted after an inactive period?

if that makes any sense at all

#5 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 09 August 2006 - 12:22 PM

As was posted, you have a field that contains a timestamp of when they last clicked something. Then where ever you want to show the users online, you only select those where they last clicked in, say, the last 5 minutes.

There is no need to delete anything at all.

#6 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 09 August 2006 - 12:27 PM

ok thanks, how would i select just the last 5 minutes?

#7 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 09 August 2006 - 12:36 PM

This is somthing i have used in the past that is checked every time a user clicks on a link.

	function update() {
		$expire = date("U")-60*5; // 5 is the number of minute until expiry.
		$query = "DELETE FROM sessions WHERE time < '$expire'";
		
		mysql_connect(host, user, pass);
		
		$report = mysql_query($query) or die(mysql_error());
	}

When all else fails - Try reading the manual.

Integrated IT Systems Ltd


#8 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 09 August 2006 - 12:46 PM

What i would do is have a field on my user table called last active of something. Then, as i say, i would insert a unix timestamp on each click...

$time = time();
mysq_query("UPDATE `table` SET `lastactive`='$time' WHERE....")


then, on the users online page:
$now = time();
$fiveminsago = $now - 5*60;
$sql = mysql_query("SELECT * FROM `table` WHERE `lastactive`>'$fiveminsago'");

#9 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 09 August 2006 - 12:55 PM

Thats actually better than the way i did it as i had to have a users table and a sessions table wheras you could just search your users table for the last sign of activity not having to worry about deleting sessions and creating them.

Just update the last active field.

If there's a long way to do something guarunteed ill find it.

When all else fails - Try reading the manual.

Integrated IT Systems Ltd





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users