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 in, it directly redirect to login.php
Please help me to solve the problem
Thanks
Answer
Solution:
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
}
}
Answer
Solution:
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']);
Answer
Solution:
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 aheader()
.
...
if (logged in && redirect to is filled in){
header('Location: '.$_REQUEST['redirect_to']);
}
...
Above is pseudo-code, btw
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: use the option --with-all-dependencies (-w) to allow upgrades, downgrades and removals for packages currently locked to specific versions.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.