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. Quote Link to comment Share on other sites More sharing options...
Solution Ansego Posted March 28, 2014 Solution 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 } ?> Quote Link to comment Share on other sites More sharing options...
davidannis Posted March 28, 2014 Share Posted March 28, 2014 (edited) 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'); } ?> Edited March 28, 2014 by davidannis Quote Link to comment 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. Quote Link to comment 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'); Quote Link to comment 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). Quote Link to comment 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.