In this tutorial, we are quickly going to see how to logout the user and end the session.
For logging out a user, we use a separate file logout.php. The user is redirected to this page when he wants to log out.
Click on the links below to go to previous tutorials
<?php @ob_start(); session_start(); //destroy the session variable. unset($_SESSION['username']); echo '<h3 style="color:green;">You have been successfully logged out.</h3>'; session_destroy(); ?>
Here, first we start the session. Then the session variable $_SESSION[‘username’] which indicates whether the user is logged in or not is destroyed using the unset() command. The user is displayed the *successfully logged out* message. The session is then destroyed using the session_destroy() command because session is no longer needed.
Please feel free to download the source code for reference.
This content has been helpful to you?
Thanks for contributing!