简体   繁体   中英

PHP - Check if $_SESSION exists for multiple variables if not redirect

I have the following PHP code:

if(!isset($_SESSION)) {
    session_destroy();
    header('Location: register.php'); //redirect to the orginal form
} else {
    $businessOwnerID = $_SESSION['business_owner_id']; //The Business owner ID
    $mobileValidation = $_SESSION['mobile_validation'];
}

I am not unserstanding why it isn't redirecting to register.php

I tried destroying all sessions by doing session_destroy(); but it did not work either

Since you obviously don't really want to just check if there is a session (there pretty much always will be) but if particular session attributes are set, you should be doing it this way:

if(!isset($_SESSION['business_owner_id'], $_SESSION['mobile_validation']))
{
   session_destroy();
   header('Location: register.php'); //redirect to the original form
} 
...

The $_SESSION super global will always be set after calling session_start() .

Instead, you should check if a particular value is set:

if(!isset($_SESSION['business_owner_id'])) {
    session_destroy();
    header('Location: register.php'); //redirect to the orginal form
} else {
    $businessOwnerID = $_SESSION['business_owner_id']; //The Business owner ID
    $mobileValidation = $_SESSION['mobile_validation'];
}

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