Jump to content

Redirect to previous page after login not working


Go to solution Solved by mac_gyver,

Recommended Posts

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!


  • Solution

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.

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.