简体   繁体   English

无法使用php会话重定向到另一个页面

[英]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. 因此,我的login.php上有此代码,当用户名和密码与数据库匹配时,它将重定向到另一个页面,该页面需要先登录才能访问。

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(); 您是否声明了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 我建议验证是否实际上是使用isset设置的,然后为空

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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