phpsessionundefined-variable

PHP: user is successfully logout but after logout refresh home page it shows that undefined variable:


Email is the user session email after successfully logout there is no email exist but on index page is shows undefined index as there is no email after logout.

Notice: Undefined index: email in C:\xampp\htdocs\CMS\index.php on line 9
    <?session_start();
?>
<?php
    require("conection/connect.php");   
    $username=$_SESSION['email'];
    $sel= "select * from users where email= '$username'";
    $run_name = mysqli_query($con, $sel);
    $row_user=mysqli_fetch_array($run_name);
    $name=$row_user['name'];
    $userid=$row_user['user_id'];
    $teacherid=$row_user['teacher_id'];
    $userid=$row_user['stu_id'];
    $usertype=$row_user['type'];
?>

Solution

  • Just verify that it's set. That will probably be useful to you anyway, because I assume if it isn't set you may want to do something different.

    <?php
    
    if (!isset($_SESSION['email'])) {
        // maybe redirect to login?
        // or at least don't show them logged-in user type stuff
    }
    
    // your code (in an else block if you didn't redirect in the if)