简体   繁体   English

基于用户会话的重定向周期

[英]redirecting cycle based on user session

I have a problem with redirecting to a page. 我在重定向到页面时遇到问题。 It seems like it is redirecting itself multiple times. 似乎它多次重定向自身。 I have 我有

if(!isset($_SESSION)){
header("location:index.php");
exit();
}

in my header. 在我的标题中 and my header is included in my index page and everywhere else. 我的标头包含在我的索引页和其他地方。 I know that since it is in the header, it will be redirected to index.php then it will the script again and over and over which is giving me problems. 我知道,因为它在标题中,它将被重定向到index.php,然后它将再次脚本反复执行,这给了我一些问题。

How would I prevent it to stop doing it right after redirecting? 我如何防止它在重定向后立即停止执行? Or is there another way to check if user is logged in, if not, go to index.php? 还是有另一种方法来检查用户是否已登录,如果没有,请转到index.php?

Thanks! 谢谢!

You should not check for the SESSION array itself to be set in its entirety, for starter. 对于初学者,您不应该检查SESSION数组本身是否已完整设置。 You should just assign one or two indexes and check against them. 您应该只分配一个或两个索引并对其进行检查。 In this way, you won't ever escape the redirect, since you're not even given the possibility to assign a SESSION.. 这样,您将永远不会逃脱重定向,因为您甚至都没有可能分配SESSION。

You need to assign something like $_SESSION['authorized'] = TRUE; 您需要分配类似$_SESSION['authorized'] = TRUE; upon succesful login , and than, on every page, check if this index is set, is TRUE, and act accordingly. 成功登录后 ,然后在每个页面上检查此索引是否已设置,为TRUE并采取相应措施。 Also, you should redirect to a login page, or something like that...you should give the possibility to actually login to the user! 另外,您应该重定向到登录页面或类似的内容...您应该可以实际登录用户! Unless your login form isn't in the index page, of course, but you didn't specify that information... 当然,除非您的登录表单不在索引页面中,但是您没有指定该信息...

if(isset($_SESSION['authorized']) AND ($_SESSION['authorized'] === TRUE)
{
   //ok, user is logged. Redirect somewhere else? Do something specific?
}
else
{
   header("Location:index.php"); // or redirect to login.php, for example
   exit();
}

Also, remember to call session_start() as the first thing in all your pages where you want Sessions to be used. 另外,请记住在要使用会话的所有页面中首先调用session_start()

You have just a logic flaw with your if statement. if语句仅存在逻辑缺陷。

If the session handling would have been started, $_SESSION would exists. 如果将开始会话处理,则$_SESSION将存在。

If not, it does not exists. 如果不是,则不存在。

But this is independent to whether you/the user makes use of a session or not. 但这与您/用户是否使用会话无关。

So the if statement is useless. 因此, if语句是无用的。 Please read the PHP Manual to learn how sessions work in PHP . 请阅读PHP手册,以了解会话如何在PHP中工作

I figured it out, I knew that I had a if statement in my header, so while i'm trying to redirect to index.php, I still have that if statement and not having a session, i'm going to be redirecting myself in a never ending loop. 我弄清楚了,我知道我的标头中有一个if语句,所以当我尝试重定向到index.php时,我仍然有if语句但没有会话,我将要重定向自己在一个永无止境的循环中。

What I did is just created a new header for index.php 我所做的只是为index.php创建了一个新头

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

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