Jump to content

Help with Sessions


lalnfl

Recommended Posts

Are you looking for an inactivity timer?

session_start();

// 10 mins in seconds
$inactive = 600; 

$session_life = time() - $_session['timeout'];

if($session_life > $inactive)
{  session_destroy(); header("Location: logoutpage.php");     }

S_session['timeout']=time();

 

Link to comment
https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112872
Share on other sites

Sure :)

 

This starts the session:

session_start();

 

This is just telling the script how long to let them be inactive(Must be in seconds):

$inactive = 600;

 

Setting the session_life variable:

$session_life = 

 

Current Unix time:

time()

 

Subtracting the current inactive time(not completely sure):

 - $_session['timeout']

 

Saying if the session life time is greater than the $inactive time:

if($session_life > $inactive)

Do This:

 

Destroy the session:

{  session_destroy()

 

Redirect to the logout page:

header("Location: logoutpage.php");     }

If not greater than $inactive:

 

Setting session timeout to the current Unix time:

S_session['timeout']=time();

 

-edit- fixed minor mistype -edit-

Link to comment
https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112877
Share on other sites

Updated code:

<?php
session_start();

// 10 mins in seconds
$inactive = 600; 

$session_life = time() - $_SESSION['timeout'];

if($session_life > $inactive)
{  session_destroy(); header("Location: logoutpage.php");     }

$_SESSION['timeout']=time();
?>

 

Link to comment
https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112894
Share on other sites

Oops, try this one. I keep it in a file called timer.php and just include to keep it simple, just an Idea ;)

<?php
$error = 'Your session timed out.'; 
session_start();
if($_SESSION['session_count'] == 0) { 
$_SESSION['session_count'] = 1;
$_SESSION['session_start_time']=time();
} else {
$_SESSION['session_count'] = $_SESSION['session_count'] + 1;
}

$session_timeout = 1800;

$session_duration = time() - $_SESSION['session_start_time'];
if ($session_duration > $session_timeout) { 
session_unset();
session_destroy();
session_start();
session_regenerate_id(true);
setcookie("Key_WatsonN", 0, time()-3600);
setcookie("Errors", 0, time()-3600); 
setcookie("UID", 0, time()-3600);
setcookie("PWD", 0, time()-3600);
setcookie(Errors, $error, time()+120000);
$_SESSION["expired"] = "yes";
header("Location: /"); // Redirect to Login Page
} else {
$_SESSION['session_start_time']=time();
}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112973
Share on other sites

Okay I do have 1 more question, if I have a script that goes along with this one that logs the person in and out, but how about if the user closes the browser without either logging out or going over the inactivity time? How would you make it so it would show the person logged out?

Link to comment
https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1113034
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.