tags:

views:

1405

answers:

7

I just wrote a PHP login script, and what I'm trying to accomplish is that when the user click to the log out link, after they log out, regardless clicking the back button of the browser, they cannot access the page.

Here is the logout function:

//Start the Session
session_start();
session_destroy();

header("location:login.php");
exit();

I did place the following code on all the pages, and this seems not do the job:

header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
header ("Pragma: no-cache");

//Start the Session
session_start();

Any suggestions?

+3  A: 

Just redirect if there's no login $_SESSION, for example:

//on your protected pages
session_start();
if(!$_SESSION['logged']) {
    header("location:login.php");
}

This is what my logout does:

session_start();
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
 setcookie(session_name(), '', time()-42000, '/');
}
// Finally, destroy the session.
session_destroy();
karim79
+1  A: 

Check when the user is logged out if the session global is still set with the correct value.

print_r($_SESSION);

The reason for this is that you are doing a session_destroy and then a header redirect, what happens is that you force a redirect and the destroying of the session isnt written to the server that way.

Ólafur Waage
Olafur, I think I'm getting close. If I'm not mistaken is better to set the $_SESSION superglobal to nothing, like this: $_SESSION = array(); still I'm able to click the browser back button and see the page without the dynamic content though. I would like to point that my logout is a simple link to a logout.php. Instead of a link, should I make the logout a button form? See, when clicking back the browser does that, go back, is not resending the page to be processed again. Any other suggestions?
Ole Media
You should do a check that if the user is accessing the site with an empty session variable, he should be thrown out via a header() redirect.
Ólafur Waage
The problem was that I was not setting the superglobal $_SESSION['logged'] to null. So checking with print_r($_SESSION); helped me to locate the problem. Thanks.
Ole Media
+3  A: 

You can't control the workings of the client-side back button on the server. You could destroy the history data using javascript on the client.

The client can completely ignore the no-cache headers.

jmucchiello
Should then set a javascript that when clicking the logout send users to logout.php? instead of just making a link <a href="logout.php">logout</a>
Ole Media
Don't forget javascript does not have to be enabled on the client so don't rely on it.
jmucchiello
You are right. so how can I prevent users to access the site from clicking the browser back button, after they click logout?
Ole Media
A: 

I would suggest that you use HTTPS with SSL. You can close the SSL session and kick the user back out to a non-encrypted page.

Most browsers implement caching schemes differently.

For example, in Opera you can click Back and it will pull the page data directly from memory without sending any data to the server, even in the page has expired. If you hit Refresh, of course, your server would require the login.

In Internet Explorer, it's handled very differently and form data is resubmitted to the server.

Chris Thompson
A: 

It might be your session_destroy() functions. Try this:

unset($_SESSION);

Un-setting the *$SESSION variable will clear out anything stored here.

Check out unset() on PHP.net

Max Felker
A: 

I think you need to store something in the session and then check it on each page load. Here's how I've done it in the past

Login Script (simplified)

session_start()
// register necessary session variables
$_SESSION['username'] = $username;

Logout Script:

session_start();

// destroy the session and check to make sure it has been destroyed
session_destroy();
    if(!session_is_registered('username')){
     $loginMessage = 'You have been logged out.';
     include 'index.php';
     exit();
    }

// if we're still here, some bad juju happened

Top of Every Page

session_start()

// make sure user is logged in
if (!$_SESSION['username']) {
    $loginError = "You are not logged in.";
    include("index.php");
    exit();
}


Shea Daniels
A: 
$_SESSION['blah'] = '';

This works too..

Paul Janaway
Paul, Yes, this sets session variable to nothing. But the problem that I'm having is that when I click the browser back button, I can still access the previous members page. However, the member pages is shown without the dynamic content that comes from session variables. What I'm trying to accomplish is that when clicking logout, users cannot access to the site even if they click the browser back button.
Ole Media