Jump to content

[SOLVED] Changing Session variables between pages


A.White.89

Recommended Posts

I am working on a session timeout functionality and I am having it timeout at 600 seconds of inactivity.

 

This function, run at the top of each page, is:

function timeoutCheck($activeTime)
{
	$maxNoActivity = 600; // Seconds of session duration of no activity
	$difference = (time() - $activeTime);

	if($difference > $maxNoActivity)
	{
		session_destroy();
		header("Location: login.php?timeout=1&page=".$_SERVER['PHP_SELF']);
	}
}

 

Then just below this function on each page the $_SESSION['activeTime'] variable is set to time().  So theoretically, $difference should be near  2-3 seconds if I refreshed the page every few seconds.  Unfortunately, $difference always increases until 600 seconds at which time the session times out. 

 

To sum it all up, $_SESSION['activeTime'] is for some reason not changing between pages.  It is never being updated to the new time (or at least the updated $_SESSION['activeTime'] is changing but not passing to other pages in the session).  Note: All pages have session_start() at the top.

 

Suggestions?

Thanks.

Link to comment
Share on other sites

Okay.  Here is the code from above (the timeoutCheck function):

 

from loginFunction.php:

<?php
function timeoutCheck($activeTime)
{
	$maxNoActivity = 600; // Seconds of session duration of no activity
	$difference = (time() - $activeTime);
	//print $difference;

	if($difference > $maxNoActivity)
	{
		session_destroy();
		header("Location: login.php?timeout=1&page=".$_SERVER['PHP_SELF']);
	}
}
?>

 

and here is the code that rests at the top of all my pages:

 

<?php
session_start();
require "loginFunctions.php";
secure($_SESSION);
timeoutCheck($_SESSION['activeTime']);
$_SESSION['activeTime']= time();
?>


 

If I print out on any page AFTER the timeoutCheck:

<?php
print (time() - $_SESSION['activeTime']);
?>

I get 0 - which is good.  This suggests that $_SESSION['activeTime'] is changed within the page but it is not being transferred to other pages because in timeoutCheck, the variable is no different than it is when it is instantiated - which is here (in login.php):

<?php
$error = loginCheck($_POST, $_GET);
	if(trim($error) == "")
	{
		$_SESSION['userID'] = login($_POST);
		$_SESSION['activeTime'] = time(); // LOOK HERE
// The rest is not important
?>

 

Hopefully this helps.  Thanks.

Link to comment
Share on other sites

Have you ever heard of session variables not storing between pages and/or functions?
Yes, when the code is overwriting the variables or doing something else that is preventing the session from being started or register_globals are overwriting post/get/cookie/session/program variables or the sessions are not working due to session settings on the server and the hostname or paths changing in the URL's or the browser has been configured to not accept cookies.

 

You are looking for a one symptom one problem relationship and programming does not work that way. For any symptom there can be a dozen different causes and any problem can cause several different symptoms.

 

What does secure($_SESSION); do, because it could be terminating your session so that $_SESSION['activeTime'] is just an ordinary array variable in the current script.

Link to comment
Share on other sites

<?php
function secure($_SESSION)
{
	if(!($_SESSION['userID']) || ($_SESSION['userID'] == ""))
	{
		header("Location: login.php?page=".$_SERVER['PHP_SELF']);
		exit();
	}
}

function timeoutCheck($activeTime)
{
	$maxNoActivity = 600; // Seconds of session duration of no activity
	$difference = time() - $activeTime;
	print $difference;

	if($difference > $maxNoActivity)
	{
		session_destroy();
		header("Location: login.php?timeout=1&page=".$_SERVER['PHP_SELF']);
	}
}
?>

 

secure() doesn't touch $_SESSION['activeTime'] and works well to make sure a user is logged in before being able to access the page (suggesting that the $_SESSION['userID'] is being transferred fine).

Link to comment
Share on other sites

Well, it turns out the problem is because you are calling the secure() function with the $_SESSION array as a parameter.

 

$_SESSION is a superglobal and does not need to be passed into a function.

 

I can only guess that when the $_SESSION array is referenced as a parameter in the function call that it is somehow breaking the connection between the $_SESSION array and the actual session.

 

Remove $_SESSION from both the secure function definition and the function call.

 

 

Link to comment
Share on other sites

I am trying to make it logout after 10 minutes of INACTIVITY, not just 10 minutes period.  That's why I change the session variable on each page AFTER I check the value of the session variable to make sure the current time is not 600 seconds more than the last loaded page.  For some reason, that difference is always increasing, suggesting that the variable is NOT storing between pages.

Link to comment
Share on other sites

Thats wut the session lifetime is for. to log a user out after x seconds by killing the session.

 

Unless you are specifically using the time the user has been on the site, there is no need to manually log them out when you can set the lifetime of the session cookie, which will get updated when they navigate to another page or refreshing thus resetting cookie.

Link to comment
Share on other sites

No it won't -

Setting the session cookie lifetime through the session_set_cookie_params() function would have no effect on detecting a period of inactivity.

 

The session cookie lifetime determines how long the session cookie lives when the browser is completely closed.

Link to comment
Share on other sites

If your forum profile is not setup to alert you of posts made while you were replying, you should really turn that setting on, because you apparently missed the reason why your code is not working -

 

Well, it turns out the problem is because you are calling the secure() function with the $_SESSION array as a parameter.

 

$_SESSION is a superglobal and does not need to be passed into a function.

 

I can only guess that when the $_SESSION array is referenced as a parameter in the function call that it is somehow breaking the connection between the $_SESSION array and the actual session.

 

Remove $_SESSION from both the secure function definition and the function call.

Link to comment
Share on other sites

The only way to detect inactivity is really through java(script). I base my opinions on the code presented. Which just destroys a session if a specific amount of time has elapsed.

Which is exactly wut the lifetime of a cookie does. A Cookie Lifespan can only last as long as the expiry dictates.

 

session_set_cookie_params

U can use this to set the lifespan of the cookie, or you can edit your php.ini in order to shorten the lifespan of the sessions.

 

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.