proper way to logout from a session in PHP
To properly log out of a session in PHP, you can use the session_destroy() function. This function destroys all data registered to a session and clears the session cookie on the client's browser. Example:
<?php
session_start();
session_destroy();
Watch a video course
Learn object oriented PHP
You can also unset all session variables and delete the session cookie like this:
<?php
session_start();
$_SESSION = array();
setcookie(session_name(), '', time() - 42000);
session_destroy();
It's important to note that session_destroy() only destroys session data on the server, so unsetting session variables and destroying the session cookie is also recommended in order to fully log out of the session.