开发者

Redirecting back after login in PHP

I have a scenario like this. When the admin receive a mail from client about orders with a URL link to PHP form page, which only can accessed by admin login. If administrator is not logged in, the url will be redirected to the login page. After the administrator logged in successfully I need to redirect him to the URL that he received in the email.

Can anybody tell me how to do it. I read something about HTTP_REFERER, but its not working properly as if the admin is not logged i开发者_高级运维n, it directly redirect to login.php

Please help me to solve the problem

Thanks


Just try to give some example here:

originating_page.php

if(!is_admin) {
    $_SESSION["originatingpage"] = $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
    header('Location: http://'.$_SERVER["HTTP_HOST"].'/login.php');
  }
}

login.php

if(is_admin_success_login) {
  if (isset($_SESSION["originatingpage"])) {
    $originatingpage = $_SESSION["originatingpage"];
    unset($_SESSION["originatingpage"]);
    header('Location: http://'.$originatingpage);
  } else {
    //do another default action
  } 
}


Store the URL of the page the person was trying to access before redirecting to login.php. You can store it in a session variable or in the URL. Then your login.php page will know where to send the user after they're logged in.

For example, at the page requiring login:

session_start();
$_SESSION['came_from'] = $_SERVER['REQUEST_URI'];
header("Location: login.php");

And in login.php, after the user has been logged in:

session_start();
header("Location: http://" . $_SERVER['HTTP_HOST'] . $_SESSION['came_from']);


your login page can have a hidden input:

<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" />

Or in the "originating page, you can pass the current REQUEST_URI as a get variable, which in turn goes in to that same form element. Then when they submit the form you check for $_REQUEST['redirect_to'] and pass it off with a header().

...
if (logged in && redirect to is filled in){
  header('Location: '.$_REQUEST['redirect_to']);
}
...

Above is pseudo-code, btw

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜