I'm trying to set the session with Page1.php, redirect to Page2.php, and then if Page2.php is refreshed, closed, revisited i want it to display a 404 error. This is what I have:
page1.php:
session_start();
$_SESSION['time'] = 'time';
header('location: page2.php');
exit;
page2.php:
session_start();
if( !isset( $_SESSION['time'] ) ) {
header("Location:http://google.com");
} else {
echo ( "this session is ". $_SESSION['time'] );
}
How do I add the function so page2.php expires if refreshed? I just want the page2.php url to expire for the user after the browser session ends
if you want to expire the session, do that with session_destroy()
and finally call
header("HTTP/1.0 404 Not Found");
In a nutshell, once you start a session with session_start(); you will be able to access the session variables stored in $_SESSION.
And when you are done with the session, call the session_destroy() to clean up and kill the session.
for eg:
if($action =='logout') {
session_destroy();
}
PS: if you have set session.auto_start in your php.ini configuration, you dont have to call session_start() at the top of the page.
For more information on session_destroy, follow this link: http://php.net/manual/en/function.session-destroy.php
For full details on session: http://php.net/manual/en/ref.session.php
add
$_SESSION['time'] = '';
at the end of else