Skip to content

Student Listing: Adding User Logins - Logout

Creating a Logout Page that Clears out the Session and Redirects the User to the Main Page

Creating a Logout script involves checking if the user is logged in, and if they are, destroying their Session along with any created Session variables, then redirecting the user to the main Student Listing page. Create a script called logout.php and add the following code:

logout.php - Student Listing: Complete Code Listing

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?php
    // Log the user out
    ///////////////////

    session_start();

    // If the user is logged in, delete session variables
    // and redirect to the home page
    if (isset($_SESSION['user_id']))
    {
        $_SESSION = array();
        session_destroy();
    }

    // Redirect to the home page
    $home_url = dirname($_SERVER['PHP_SELF']);
    header('Location: ' . $home_url);
    exit;