开发者

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 m开发者_StackOverflowy 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.

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?

Thanks!


You should not check for the SESSION array itself to be set in its entirety, for starter. 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..

You need to assign something like $_SESSION['authorized'] = TRUE; upon succesful login, and than, on every page, check if this index is set, is TRUE, and act accordingly. 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.


You have just a logic flaw with your if statement.

If the session handling would have been started, $_SESSION would exists.

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. Please read the PHP Manual to learn how sessions work in 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.

What I did is just created a new header for index.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜