Sessions

Hi, I’ve been working on a site lately. I have the majority of it done but I’m running into an issue with online sessions. Atm I’m storing each session into the database when the user Logs in, then when they log out it deletes the session. I’ve seen several tutorials using time stamps to delete the session after a certain amount of time. But since that would end the session after 20 minutes and put the player offline. I was wondering if doing a Crontab and maybe a loop through all the sessions like every 5 minutes to check if they are still valid or not. I started up a little something, Idk if it’ll work though. Does anyone have any ideas?

	session_start();
	require_once("/*Connect to Db*/");
	$result = mysql_query("SELECT * FROM Online");
	while($row = mysql_fetch_array($result)) {		
		if(!isset($_SESSION[$row['User']])) {			
				mysql_query("DELETE FROM Online WHERE User = '".$row['User']."'");		
		}
	}

My idea for this is to reduce the default session time of 1440 seconds into 300 seconds (that’s 5 minutes) and place your player inside DIV tag so that we can constantly reload that DIV every 5 minutes using jQuery.

To set session time:

$expire = “300”;
ini_set(session_gc_maxlifetime, $expire);

To reload specific DIV, try to Google it first; use this search term “Reload specific DIV using jQuery”.

Inside your DIV you should write the codes that would check the validity of the session.

Thanks man, I looked a little bit more into how i was wanting to approach this, and the answer was so obvious. Time stamps. Then just update the time stamp lol.

	$time=time();
	$time_check=$time-60*20; //60sec * 1 = 1 min
	mysql_query("DELETE FROM Online WHERE Time < '".$time_check."'");

Then just adding the following in my Ajax function which controls probably 95% of my site.

mysql_query("UPDATE Online SET Time = '$time' WHERE User = '".$_SESSION['User']."'");

Appreciate your help though :], I was seriously like DUHHH… Lol. Hopefully this will help some one else if they’re gonna add an online list lol.

Sponsor our Newsletter | Privacy Policy | Terms of Service