Canadian Posted June 8, 2015 Share Posted June 8, 2015 Here's the goal. After the user logs in on my login.php page I either redirect them by default to the "account.php" page OR redirect them to the last protected page they were trying to access. Here's what I'm doing. Using $_SESSION["username"] I'm verifying whether or not the user is logged in with my confirm_user_logged_in function below. If they are logged in, they go to the requested page. If not, I'm setting the page they came from as the $_GET variable 'last_page'. function confirm_user_logged_in() { if(!logged_in ()) { $last_page = $_SERVER['PHP_SELF']; $last_page_trim = ltrim ($last_page, '/'); $redirect_url = "login.php?last_page=" . $last_page_trim; redirect_to($redirect_url); } } function logged_in () { return isset($_SESSION["username"]); } function redirect_to($new_location) { header("Location: " . $new_location); exit; } This part works. If I go to my login.php page from "example.php" the $_GET['last_page'] value is set and I get the following in the url string. http://example.com/login.php?last_page=example.php Next... On the login.php page I'm testing to see whether or not $_GET['last_page'] isset. if (isset($_GET['last_page'])) { $last_page = $_GET['last_page']; } else { $last_page = "account.php"; } At this point if I echo $last_page I get the following result. example.php Here's the problem. If I call my "redirect_to" function it's always sending the user to account.php. Even though $_GET['last_page'] isset AND even though $last_page is set to "example.php", I'm still being sent to account.php. Here's the redirect_to function as called. redirect_to($last_page); Why is $last_page dropping the $_GET['last_page'] value as soon as I put it into the "redirect_to" function? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/296698-redirect-to-previous-page-after-login-not-working/ Share on other sites More sharing options...
Solution mac_gyver Posted June 8, 2015 Solution Share Posted June 8, 2015 i suspect that your code is actually redirecting to where you want, but code on that page is redirecting back to the page you finally see. rather than redirecting all over the place on your site and trying to keep track of where you should be at, you can simplify all of this by making a single page (index.php) that handles everything. see the following reply for how your page should be generally laid out - http://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/?do=findComment&comment=1513104 the post method form processing code for the login functionality will authenticate the user, set the session variable that identifies him (i recommend using the user id, not the user name), then does a redirect to the same page to cause a get request for that page. the get method code for the login functionality will either produce the login form (and probably a registration link), if the visitor is not logged in, or it will produce a welcome message and a log out link if the visitor is logged in. you would just display the result from the get method login code where you want it on your page. any other content on the page, navigation, main content, ... can use the logged in state to determine what content they will produce, that will then be displayed where you want it on your page. Quote Link to comment https://forums.phpfreaks.com/topic/296698-redirect-to-previous-page-after-login-not-working/#findComment-1513429 Share on other sites More sharing options...
Canadian Posted June 8, 2015 Author Share Posted June 8, 2015 i suspect that your code is actually redirecting to where you want, but code on that page is redirecting back to the page you finally see. Ahh! You're a genius. That's exactly what was happening. Thanks for pointing that out. rather than redirecting all over the place on your site and trying to keep track of where you should be at, you can simplify all of this by making a single page (index.php) that handles everything. see the following reply for how your page should be generally laid out - http://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/?do=findComment&comment=1513104 the post method form processing code for the login functionality will authenticate the user, set the session variable that identifies him (i recommend using the user id, not the user name), then does a redirect to the same page to cause a get request for that page. I'm following what you are saying, but putting that into code is another thing. My post method is currently authenticating the user, my session variable is identifying the user, but I'm not sure how I'd redirect the user to the page they came from. Do you have any examples you could direct me to for this? the get method code for the login functionality will either produce the login form (and probably a registration link), if the visitor is not logged in, or it will produce a welcome message and a log out link if the visitor is logged in. you would just display the result from the get method login code where you want it on your page. any other content on the page, navigation, main content, ... can use the logged in state to determine what content they will produce, that will then be displayed where you want it on your page. Thanks again for the help! Quote Link to comment https://forums.phpfreaks.com/topic/296698-redirect-to-previous-page-after-login-not-working/#findComment-1513484 Share on other sites More sharing options...
mac_gyver Posted June 8, 2015 Share Posted June 8, 2015 the linked to forum reply contains example code showing how to do the suggested redirect. Quote Link to comment https://forums.phpfreaks.com/topic/296698-redirect-to-previous-page-after-login-not-working/#findComment-1513488 Share on other sites More sharing options...
Canadian Posted June 8, 2015 Author Share Posted June 8, 2015 the linked to forum reply contains example code showing how to do the suggested redirect. Ok thank you! I will have to re-read that then. All the best, Quote Link to comment https://forums.phpfreaks.com/topic/296698-redirect-to-previous-page-after-login-not-working/#findComment-1513494 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.