RobMs Posted April 5, 2010 Share Posted April 5, 2010 Hey all, I have a "header" page that I include in every page. If I want to put a script in their to redirect to a page if not logged in, how can I do this without creating a redirect loop? (which would be caused because it would be included in the login page too). for example using: header ("Location: index.php"); in index.php would create a redirect loop. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/197580-redirect-in-header/ Share on other sites More sharing options...
oni-kun Posted April 5, 2010 Share Posted April 5, 2010 Should be fairly simple and straightforward. <?php session_start(); //You need this at top on every page using sessions. if(!isset($_SESSION['id'])) { header('login.php'); exit; //Required } That'll redirect them to wherever you wish if their session isn't set (IE: Their ID). Change 'id' to whatever session variable you use for them when logged in. Quote Link to comment https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1036937 Share on other sites More sharing options...
RobMs Posted April 5, 2010 Author Share Posted April 5, 2010 But that will cause a loop on login.php? Quote Link to comment https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1036938 Share on other sites More sharing options...
oni-kun Posted April 5, 2010 Share Posted April 5, 2010 But that will cause a loop on login.php? You could place unique code on just login.php: if (isset($_SESSION['id'])) { print "You are already logged in!"; exit; } else { //<form .. log in form. } Of course that is only a crude example, but as you see it's fairly simple to implement. Once the session is set, It will not let them 're-login' and it will bypass the checks on other pages as the session's ID value will be there. Quote Link to comment https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1036939 Share on other sites More sharing options...
RobMs Posted April 5, 2010 Author Share Posted April 5, 2010 Thanks, but I know how to check if someone is logged in, my question relates to the redirect and how to stop it looping. Quote Link to comment https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1037097 Share on other sites More sharing options...
Tazerenix Posted April 5, 2010 Share Posted April 5, 2010 in any page where you want the user to be logged in add header("Location: login.php") if they aren't logged in. Then in login.php if the user is logged in redirect to index.php (since your logged in this wont be a recuring loop) or if they aren't print the form Quote Link to comment https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1037098 Share on other sites More sharing options...
RobMs Posted April 5, 2010 Author Share Posted April 5, 2010 Thanks, I still dont think you all understand. ok file header.php is included in EVERY page, this would include login.php This means: if (!loggedIn()) { header ("Location: login.php"); exit; } would be included in login.php also, thus login.php would loop. I want to be able to stop it looping in this page. Quote Link to comment https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1037192 Share on other sites More sharing options...
the182guy Posted April 5, 2010 Share Posted April 5, 2010 You can check for the login.php filename and if found, then don't redirect to login, like if(!loggedIn() && basename($_SERVER['PHP_SELF']) != 'login.php') // if not logged in AND the current script is not login.php { header ("Location: login.php"); exit; } Quote Link to comment https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1037201 Share on other sites More sharing options...
RobMs Posted April 5, 2010 Author Share Posted April 5, 2010 Great, thank you :-) Quote Link to comment https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1037208 Share on other sites More sharing options...
Tazerenix Posted April 6, 2010 Share Posted April 6, 2010 Thanks, I still dont think you all understand. ok file header.php is included in EVERY page, this would include login.php This means: if (!loggedIn()) { header ("Location: login.php"); exit; } would be included in login.php also, thus login.php would loop. I want to be able to stop it looping in this page. Sorry i didnt realise Quote Link to comment https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1037504 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.