Sessions and compairing time variables
Posted: Sat Feb 15, 2003 9:22 am
This is probably a simple task but I am finding not so, I want to set a session then check with other pages that the session is set and not out of date. The problem I am having is compairing the date and time the session is set to the date and time the user goes to other pages, if the user goes to another page within the hour then fine and if not they are directed to the login page.
Now I know that with the code below I am compairing 2 strings but I understood that while working with time that was the case, this is apparent that it is probably not the case so anyone give me any pointers on how to do this?
Thanks in advance for aby help.
I set the session on login with the following:
and then I check the session with the following:
Now I know that with the code below I am compairing 2 strings but I understood that while working with time that was the case, this is apparent that it is probably not the case so anyone give me any pointers on how to do this?
Thanks in advance for aby help.
I set the session on login with the following:
Code: Select all
<?php
$session_timeout = date("y-m-d H:i:s", time()) + 3600; // + 1 hour
// Set the cookie
session_name("OPL_login");
session_start();
session_register("crypted_password");
session_register("session_timeout");
$HTTP_SESSION_VARS["crypted_password"]=$crypted_password;
$HTTP_SESSION_VARS["session_timeout"]=$session_timeout;
$timed_out = "Your session has timed out, ";
?>Code: Select all
<?php
$session_name = $_REQUEST[session_name()]; // Get session name
$session_time = $_SESSION[session_timeout]; // Get session name
$time_now = date("y-m-d H:i:s", time());
if (isset($session_name)) { // Session was set
if ($time_now > $session_timeout) { // Session is within the time allowed (1 hour)
$session_error = $timed_out."you will need to log in again";
header ("location: userlogin.php?session_error=$session_error");
}else{
echo "User has logged in within the hour";
}
}else{ // Session has not been set
$session_error = "You need to log in to gain access to the page you are attemping to load";
header ("location: userlogin.php?session_error=$session_error");
}
?>