简体   繁体   中英

Cannot redirect to another page with php session

So I have this code on my login.php that when the user and password matches with the database it will redirect to another page which requires logging in first before it can be accessible.

Here is my code in login

            $_SESSION['userlog'] = true;
            header('location: table.php');

And here is my code that requires logging in before the page become accessible.

           if(empty($_SESSION['userlog']))
           {
           header('LOCATION: login.php');
           }

The problem is when I click on login, it does not redirect in the next page. Any idea what is wrong or missing in my code?

Did you declare session_start(); at the top of the page to enable sessions?

I would recommend validating if it was in fact set using isset and then empty

if(!isset($_SESSION['userlog']) && empty($_SESSION['userlog']))
{
    header('LOCATION: login.php');
}

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