Jump to content
Larry Ullman's Book Forums

How To Create A Target Url Redirect After User Has Successful Logged In.


Recommended Posts

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

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

 Share

×
×
  • Create New...