100yen Posted November 20, 2013 Share Posted November 20, 2013 Hi, Here the case is when user clicks a link, if he/she is not signed in yet, then he/she will be redirected to a login page. After the user successfully logged in, user should be redirected to the page url he/she intended to visit. I have no clue how to do this, but I found article online suggesting to store url into $_SESSION array before user has logged in. So I tried to store what url visitor clicked into $_SESSION['targetPage'], then redirect to login page. When user does a successful login, then $_SESSION ['targetPage'] is plugged into header function to redirect user back to the page intend. This method doesn't work, but I think the direction might be right. So please kindly give me some thought how to make this working. thank you very much! if(!isset($_SESSION['user_id'])){ // try storing the url that user clicked in $_SESSION $_SESSION['targetPage'] = $_SERVER['REQUEST_URI']; $url = 'login_page.php'; header("Location: $url"); exit(); } else { // save the session user id into $user_id $user_id = $_SESSION['user_id']; } Link to comment Share on other sites More sharing options...
100yen Posted November 20, 2013 Author Share Posted November 20, 2013 oh,, just figure out the right method to do. Thanks anyway. In case someone else looking for something, I post the code here $_SESSION['redirect_to'] = $_SERVER['REQUEST_URI']; header("Location: http://example.com/login.php"); exit(); // On successful login $redirect = $_SESSION['redirect_to']; // unset the session var unset($_SESSION['redirect_to']); header("Location: http://example.com/$redirect"); exit(); source: http://stackoverflow.com/questions/7004530/redirect-to-page-user-was-trying-to-get-to-after-logging-in Link to comment Share on other sites More sharing options...
Larry Posted November 21, 2013 Share Posted November 21, 2013 Excellent. Kudos for figuring it out and thanks for sharing the solution! Link to comment Share on other sites More sharing options...
100yen Posted November 21, 2013 Author Share Posted November 21, 2013 Excellent. Kudos for figuring it out and thanks for sharing the solution! Thank you Larry . Excellent books helps a lot . Can't wait to buy your new Effortless E-Commerce with PHP and MySQL (2nd Edition). Link to comment Share on other sites More sharing options...
Larry Posted November 21, 2013 Share Posted November 21, 2013 Thanks!!! Link to comment Share on other sites More sharing options...
Recommended Posts