0

base on my question above, I click the logout button at is redirect to the initial page index.php. At the index.php, when I pressed the browser back button, it will display message:

"Undefined index: login_user in C:\inetpub\wwwroot\ebooking\pages\dashboard\admin\dashboard_admin.php on line 6"

when I once again press the back button, it will redirect back to index.php. Below is my code:

index.php

<?php
    include("config/configPDO.php");
    session_start();

    $msg = ""; 
    if(isset($_POST['submitBtnLogin'])) {
    $User_ID = trim($_POST['Email']);
    $email=explode('@',$User_ID);
    if (is_array($email)){
        $User_ID=$email[0];
    }
    $Pwd = trim($_POST['Pwd']);
    if($User_ID != "" && $Pwd != "") {

        $ldap_dn = "TOPPOP\\".$User_ID;
        $ldap_password = $Pwd;

        $ldap_con = ldap_connect("ldap://xxx.xx.xx.xx:xxx");
        ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);

        if(@ldap_bind($ldap_con,$ldap_dn,$ldap_password)){;
            try {

                $records = $conn->prepare("SELECT Email, Role_ID, Pwd FROM Staff WHERE User_ID = :User_ID ");
                $records->execute(
                    array(  
                    'User_ID'     =>    $User_ID,
                    )  
                );
                $results = $records->fetch(PDO::FETCH_ASSOC);

                $message = '';

                if($results && count($results) > 0 ){
                    $_SESSION['login_user'] = $results["Email"];
                    if($results["Role_ID"] == "2"){ 
                        header("location: pages/dashboard/admin/dashboard_admin.php");
                    }else if ($results["Role_ID"] == "3"){ 
                        header("location: pages/dashboard/super_admin/dashboard_super_admin.php");
                    }else if ($results["Role_ID"] == "1"){ 
                        header("location: pages/dashboard/normal_user/dashboard_normal_user.php");
                    }
                } else {
                    echo "
                    <script>alert('You're not authorized to use this system')</script>
                    <script>window.location = 'index.php'</script>
                    ";
                }

            } catch (PDOException $e) {
                echo "Error : ".$e->getMessage();
            }
        } else{ 
        echo "
        <script>alert('Invalid Email or Password')</script>
        <script>window.location = 'index.php'</script>
        ";
        }

    } else {
        $msg = "Both fields are required!";
    }
}
?>

dashboard_admin.php (contain logout)

<?php

require_once "../../../config/configPDO.php";
require_once "../../../config/check.php";
$Email = $_SESSION['login_user'];   //line 6

?>

check.php

<?php
session_start();

if(isset($_SESSION['login_user']) === false){
    header("Location: logout.php");
}

?>

logout.php

  <?php
     session_start();

     session_destroy();

     header("Location: ../index.php");
  ?>
Serghei Leonenco
  • 3,478
  • 2
  • 8
  • 16
  • Reread https://www.php.net/manual/en/function.session-destroy.php. I don’t think you need to delete the session; just set `$_SESSION = array ();` – Tim Morton Jan 21 '20 at 04:27
  • You simply cannot assume that `$_SESSION['login_user']` will always be initialized. You must use `isset()` to avoid this type of buggy code. – Sherif Jan 21 '20 at 04:34
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Greg Schmidt Jan 21 '20 at 05:16

2 Answers2

1

Just check first if login_user is set in the sessions, see code below

isset($_SESSION['login_user']) // will return boolean
Qonvex620
  • 3,819
  • 1
  • 8
  • 15
1

Undefined index in dashboard_admin.php ont he 6th line. The error was caused by trying to get undefined index of superglobal array $_SESSION - it didn't exists. Next operation gives you ability to check: If index login_user isset in $_SESSION => use value from $_SESSION['login_user'] otherwise use ''.

$Email = $_SESSION['login_user'] ?? '';   //line 6

or

$Email = isset($_SESSION['login_user']) ? $_SESSION['login_user'] : '';   //line 6
Mully
  • 233
  • 1
  • 10