Redirecting to previous page after login?
A common way to do this is to pass the user's current page to the Login form via a $_GET
variable.
For example: if you are reading an Article, and you want to leave a comment. The URL for comments is comment.php?articleid=17
. While comment.php
is loading, it notices that you are not logged in. It wants to send you to login.php
, like you showed earlier. However, we're going to change your script so that is also tells the login page to remember where you are:
header("Location:login.php?location=" . urlencode($_SERVER['REQUEST_URI']));
// Note: $_SERVER['REQUEST_URI'] is your current page
This should send the user to: login.php?location=comment.php%3Farticleid%3D17
. login.php
should now check to see if $_GET['location']
is populated. If it is populated, then send the user to this location (in this case, comment.php?articleid=17
). For example:
// login.php
echo '<input type="hidden" name="location" value="';
if(isset($_GET['location'])) {
echo htmlspecialchars($_GET['location']);
}
echo '" />';
// Will show something like this:
// <input type="hidden" name="location" value="comment.php?articleid=17" />
// login-check.php
session_start();
// our url is now stored as $_POST['location'] (posted from login.php). If it's blank, let's ignore it. Otherwise, let's do something with it.
$redirect = NULL;
if($_POST['location'] != '') {
$redirect = $_POST['location'];
}
if((empty($username) OR empty($password) AND !isset($_SESSION['id_login']))) {
$url = 'login.php?p=1';
// if we have a redirect URL, pass it back to login.php so we don't forget it
if(isset($redirect)) {
$url .= '&location=' . urlencode($redirect);
}
header("Location: " . $url);
exit();
}
elseif (!user_exists($username,$password) AND !isset($_SESSION['id_login'])) {
$url = 'login.php?p=2';
if(isset($redirect)) {
$url .= '&location=' . urlencode($redirect);
}
header("Location:" . $url);
exit();
}
elseif(isset($_SESSION['id_login'])) {
// if login is successful and there is a redirect address, send the user directly there
if($redirect) {
header("Location:". $redirect);
} else {
header("Location:login.php?p=3");
}
exit();
}
Gotchas
You should run some validation against $_GET['location']
before sending the user there. For example, if I tell people who use your site to click on this link: login.php?location=http%3A%2F%2Fmalice.com%2Fevilpage.php
... then they will be sent to a foreign URL that will try to do something bad.
Always make sure to use urlencode
when passing URLs as $_GET
parameters. This encodes special URL characters (such as ?
, &
, and %
) so that they don't break your url (e.g.: login.php?location=comment.php?id=17
<- this has two ?
's and will not work correctly)
redirect user to previous page after login
You can store url inside a session may, $_SESSION['visited_page'] inside header. Every time user visit different url this session value updated. During logout, you can assign url inside $_SESSION['visited_page'] to $_SESSION['last_visited'] and unset $_SESSION['visited_page']. Now you have the url user last visit. I hope you get my point.
Redirect to the last page requested after login
I think you should try HTTP_REFERER here to redirect on last visited page. for that set a hidden field in your login form.
HTML :-
<input type="hidden" name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" />
and get redirurl
value in form post.
PHP :-
if(isset($_REQUEST['redirurl']))
$url = $_REQUEST['redirurl']; // holds url for last page visited.
else
$url = "student_account.php"; // default page for
header("Location:$url");
Or if you are using session then please ensure that you start session session_start() on that page. otherwise session will break and it couldn't save your desired URL.
How to go back to previous page after successful login
In the previous page
add these:
session_start();
require_once("./include/membersite_config.php");
if (!$fgmembersite->CheckLogin())
{
$_SESSION['redirect_url'] = $_SERVER['PHP_SELF'];
header('Location: login.php');
exit;
}
login page
session_start();
require_once("./include/membersite_config.php");
if(isset($_POST['submitted']))
{
if($fgmembersite->Login())
{
$redirect_url = (isset($_SESSION['redirect_url'])) ? $_SESSION['redirect_url'] : '/';
unset($_SESSION['redirect_url']);
header("Location: $redirect_url", true, 303);
exit;
}
}
Redirecting back after login in PHP
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
}
}
Related Topics
Initializing PHP Class Property Declarations With Simple Expressions Yields Syntax Error
How to Generate a Screenshot of a Webpage Using a Server-Side Script
With "Magic Quotes" Disabled, Why Does PHP/Wordpress Continue to Auto-Escape My Post Data
Upload Multiple Files With PHP and Jquery
PHP How to Get Local Ip of System
PHP - How to Merge Arrays Inside Array
Assigning the Return Value of New by Reference Is Deprecated
How to Use the PHP That Brew Installed
How to Setup Elasticsearch Index Structure With Multiple Entity Bindings
Replace Last Occurrence of a String in a String
Difference Between 2 Dates in Seconds
PHP Get Values from Json Encode
Best Way to Automatically Remove Comments from PHP Code