I am developing a web application with PHP where a user will be able to have his or her own account, and the session that keeps track of the user is stored in a MySQL database. Now, after searching for an answer on how to implement this, I find that many people like to both use session_destroy()
and unset the cookie. Why - wouldn't session_destroy() be enough on its own? Even the PHP manual says "In order to kill the session altogether, like to log the user out, the session id must also be unset."
My reasoning: After the user has logged out, and happens to visit just one more page on your site before leaving, the PHP script checking if the user is logged in or not will call session_start(), setting a new session cookie for the user anyway. Here's how it might look like:
// here we include some scripts and make some instances we'll need
require_once("database.php");
require_once("session.php");
$database_connection = new DB_Connection();
$session = new Session($database_connection);
// here a session cookie is sent to a user, even if he or she isn't logged in
session_start();
// finally we check if the user is logged in
$log_isLogged = false;
if(isset($_SESSION['member_id'], $_SESSION['username'])){
$log_member_id = $_SESSION['member_id'];
$log_username = $_SESSION['username'];
$log_isLogged = true;
}
Sure, it is nice for when the user knows about this fact, and leaves the site before a new cookie might be set. But some sites even redirect you to a new page directly after a logout, resulting in a new session cookie - undoing what you just did.
Is my reasoning flawed in some way, or does it not really matter if you unset a session cookie or not? Maybe most developers just think along the lines that it at least can't hurt to unset it?
I am not a native speaker, so I apologize in advance for any typos and grammatical errors.