Max Hudson
Max Hudson

Reputation: 10226

Sessions not destroying

My 'log in' code

$query = mysql_query("SELECT * FROM trade2_users WHERE email = '".mysql_real_escape_string($_POST['email'])."' AND pass = '".mysql_real_escape_string($_POST['password'])."'");
if(mysql_num_rows($query) > 0){
$row = mysql_fetch_assoc($query);
session_start();
$_SESSION['name'] = $row['name'];
header("Location: index.php");
}

My 'log out' code

session_start();
session_destroy();
header("Location: log_in.php");

For whatever reason my log out code isn't working (isn't really destroying the session). $_SESSION['name'] is still set after the log out script runs.

Upvotes: 0

Views: 69

Answers (3)

BigToach
BigToach

Reputation: 580

I typically over-write the values instead of trying to remove the session.

foreach($_SESSION as $key => $value) {
    unset($_SESSION[$key]);
}

Upvotes: 0

Phil
Phil

Reputation: 165058

From the documentation

In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.

Example #1 on that same page shows you exactly what to do.

Upvotes: 1

Kim Bryan Barcelona
Kim Bryan Barcelona

Reputation: 94

Try using :

session_unset();
header("Location: log_in.php");

Upvotes: 2

Related Questions