Logging out a session properly in PHP
Logging out a user in PHP, the right way.
The code, explained
<?php
// Recover the user's session from the server
session_start();
// Free all session variables:
// Do NOT unset the whole $_SESSION with unset($_SESSION) as this will disable the registering
// of session variables through the $_SESSION superglobal. (http://php.net/manual/en/function.session-unset.php)
session_unset();
// Destroy all
session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);
?>