Jump to content

Life of a Session


doubledee

Recommended Posts

Please refresh my memory...

 

What is the life of a Session?

 

If my script creates the following...

	$_SESSION['loggedIn'] = $loggedIn;

 

...then what is it when I close my browser window?

 

I believe it dies.

 

Is that true?

 

Is that *always* true?

 

 

Debbie

 

Link to comment
Share on other sites

print_r(session_get_cookie_params());

 

works as well.

 

I am trying to find a way to update my database - without using JavaScript - if a User leaves my website by closing their browser.

 

Specifically, when someone reading an Article looks at those who have posted Comments, I don't want any Users who have closed their browsers to still appear as being "Online".

 

I was thinking one way to do this using PHP only would be to look for $_SESSION['loggedIn'] = $loggedIn; before I display who made Comments, and if $_SESSION['loggedIn'] =  then have my script update the database to mark the User as "Offline".

 

Would that work?!

 

 

Debbie

 

 

Link to comment
Share on other sites

<?php 
// 7200 = Time in seconds for the cookie to be active; set to "0" for it to be active until the browser closes
// "/" = Where this cookie can access, currently set to root 
// ".site.com" = allow this cookie to access current domain and it's sub-domains; remove the first "." to only allow with in the domain and not subdomain
// FALSE = This cookie doesn't require a Secure Network 
session_set_cookie_params(7200,"/",".site.com",FALSE); 
session_start(); 
?>

Link to comment
Share on other sites

That's why most sites have users as "recently online", where once they are logged in, viewing any webpage will update the user's "last_activity" value and the "Who's Online" box just shows the users who have a last_activity value within the last 5-10 minutes. Of course it doesn't account for someone who sits and reads one web page for 20 minutes, but there's no way to monitor idle activity(that I know of)

Link to comment
Share on other sites

That's why most sites have users as "recently online", where once they are logged in, viewing any webpage will update the user's "last_activity" value and the "Who's Online" box just shows the users who have a last_activity value within the last 5-10 minutes. Of course it doesn't account for someone who sits and reads one web page for 20 minutes, but there's no way to monitor idle activity(that I know of)

 

I'm determined to get the behavior I want without Ajax!  8)

 

How about this...

 

When I display User Comments, for each Poster I check the database to see if their "logged_in" field is set to "1" AND I also look at the "last_activity" field and compare that against the current system time().  If more than, say 4 hours, have passed, then I run an UPDATE on the User's record and change "logged_in" to "0" which would log the Poster out from an "Online/Offline" standpoint, but not log them out since that is dictated by their own personal SESSION.

 

I'm looking at my code, and I think this would work, except I don't know if it is possible with my Prepared Statements to run an UPDATE query inside this code...

// ********************************
// Display Comments on Article.		*
// ********************************
while (mysqli_stmt_fetch($stmt2)){
	// Set Photo Label.
	$photoLabel = (empty($photoLabel) ? $username : $photoLabel)

	echo '<div class="post">';

	// ********************
	// Display User Info.	*
	// ********************
	echo '	<div class="userInfo">
				<a href="#" class="username">
					<strong>' . nl2br(htmlentities($username, ENT_QUOTES)) . '</strong>
				</a>';

	if ($loggedIn==1){
		//<!-- Member Online -->
		echo '		<img src="/images/Light_Green_10.png" width="10"
						alt="Member Status: Online"
						title="Green Light.  Credit: Someone, Wikimedia Commons." />
					<br />';
	}else{
		//<!-- Member Offline -->
		echo '		<img src="/images/Light_Gray_10.png" width="10"
						alt="Member Status: Offline"
						title="Gray Light.  Credit: Someone, Wikimedia Commons." />
					<br />';
	}

 

Hope that makes sense?!

 

 

Debbie

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.