Redirecting cycle based on user session?

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..

Up vote 0 down vote favorite share g+ share fb share tw.

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. 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! Php session redirect header logout link|improve this question asked Oct 1 '11 at 7:01andrewliu642211 66% accept rate.

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.

I just did $_SESSION to save time, I did use something like true or false, but the problem is that its being redirected at a never ending loop. – andrewliu Oct 1 '11 at 7:23 It's a matter of architecture, the check should act that way, but you have some page to be "free" from the checking, or allowing a login page that can be accessed by everyone (so no Session check here! ) – Damien Pirsy Oct 1 '11 at 7:39.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions