简体   繁体   中英

Notice: Undefined index: session PHP

This error only shows if user is not logged-in. I understand there are already a couple of errors like this, ive tried my best to test them all. but i still cant figure this out.

Notice: Undefined index: user_session in ..index.php on line 10

<?php 
session_start();

if (session_status() == PHP_SESSION_NONE){
    header('Location: index.php');
}

if (session_status() == PHP_SESSION_ACTIVE) {
   $session = $_SESSION['user_session']; //line 10
}

When user logs in , you have to set this $_SESSION["user_session"]=$value; first then only you will be able to access it. You are trying to access a variable which you haven't set anywhere. So its throwing undefined index.

If you are looking to fix this error message then please try following code, Your code changes like this:

<?php 
session_start();

if (session_status() == PHP_SESSION_NONE){
header('Location: index.php');
}

if (session_status() == PHP_SESSION_ACTIVE) {
if( isset($_SESSION['user_session']) && !empty($_SESSION['user_session']) ) 
$session = $_SESSION['user_session']; //line 10
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM