roldahayes Posted March 28, 2014 Share Posted March 28, 2014 Hi, Can I have some advise on the best way to achieve this task please. I need a way to redirect to a different version of a page once a user enters a specific code. I.e, The normal page is "basket.php" - once the user enters the code into a box on that page, it automatically jumps to "basket-trade.php" The code would need to keep them on the new version of the page everytime they visited the original, until they log out. I would need a way to add new codes (That do the same job) for each user that I want this to work for. Any advise would be appreciated, Cheers. Link to comment https://forums.phpfreaks.com/topic/287370-redirect-to-a-page-if-logged-in/ Share on other sites More sharing options...
Ansego Posted March 28, 2014 Share Posted March 28, 2014 Hi, I've not worked with php sessions much, but I am assuming they would be all similar, Maybe something like this: <?php if ($_SESSION['mysession'] == 1){ // REDIRECT }else{ // REDIRECT } ?> Link to comment https://forums.phpfreaks.com/topic/287370-redirect-to-a-page-if-logged-in/#findComment-1474318 Share on other sites More sharing options...
davidannis Posted March 28, 2014 Share Posted March 28, 2014 To expand on Ansego's answer at the top of basket.php try <?php session_start(); if ($_SESSION['mysession'] == 1){ header ('Location: http://mydomain.com/basket-trade.php'); } ?> Link to comment https://forums.phpfreaks.com/topic/287370-redirect-to-a-page-if-logged-in/#findComment-1474323 Share on other sites More sharing options...
mac_gyver Posted March 28, 2014 Share Posted March 28, 2014 or instead of having and maintaining two different pages of code, just change what your one page does using logic depending on if the visitor is logged in or not. Link to comment https://forums.phpfreaks.com/topic/287370-redirect-to-a-page-if-logged-in/#findComment-1474324 Share on other sites More sharing options...
roldahayes Posted April 9, 2014 Author Share Posted April 9, 2014 Thanks for the advice, this is how I solved it. if(isset($_REQUEST['txtLogin'])) { session_start(); $_SESSION['stored_username']=$stored_username; } if(isset($_SESSION['stored_username'])) header('Location: basket-trade.php'); else header('Location: basket.php'); Link to comment https://forums.phpfreaks.com/topic/287370-redirect-to-a-page-if-logged-in/#findComment-1475477 Share on other sites More sharing options...
Jacques1 Posted April 9, 2014 Share Posted April 9, 2014 Not quite. You must stop the script after the redirect. Otherwise, it will happily keep running, which is a very common security vulnerability. Putting the session_start() into an if statement is also poor practice, because the session will be uninitalized if the condition isn't met. That means every attempt of accessing $_SESSION will throw a notice. Last but not least, get rid of the $_REQUEST variable and properly fetch the data from either the URL ($_GET) or the request body ($_POST). Link to comment https://forums.phpfreaks.com/topic/287370-redirect-to-a-page-if-logged-in/#findComment-1475483 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.