Ole Media
Ole Media

Reputation: 1642

Destroying Sessions in database with PHP

For a web application, I decided to store the sessions variables into a database. Everything is working like I want except for the logout, in which I thought that with a simple session_destroy() will do the job but is not.

As I mention all the session information is stored in the database, but I notice that when calling session_destroy(); it does delete the session from the database but is not deleted from the server. I notice that if I do not close the browser, every time I loggin I get the same session id, therefore if I loggin with different username, I still get the same session id. How can I delete server session?

This is a function within a class that I use to destroy the session:

 function destroy( $id ) {

  // Build query
  $newid = mysql_real_escape_string($id);
  $sql = "DELETE FROM sessions_table WHERE session_id = '{$newid}'";

  mysql_query($sql);

  return TRUE;

}

Any advice please?

Upvotes: 1

Views: 192

Answers (1)

merkushin
merkushin

Reputation: 481

php-manual:

"session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

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."

Upvotes: 1

Related Questions