I have a cart products page, if a person clicks on a product add to cart butt开发者_高级运维on they will be redirected to the login page.
After a successful login, I need to send the user back to same products page.
A simple solution would be to store the "return" URL in a session variable before you kick to the login page. The login page would check for the presence of the session variable and then unset it prior to using a header location re-direct to return the user to the URL in question.
For example on the login page you'd use:
// Successfully logged in...
$destURL = $_SESSION['kickurl'] ? $_SESSION['kickurl'] : '/index.php';
unset($_SESSION['kickurl']);
header('Location: ' . $destURL);
exit();
You should track the url of each page :
as :<?php $_SESSION['url'] = $_SERVER['REQUEST_URI']; ?>
and now use that one :
<?php session_start();
if(isset($_SESSION['url']))
$url = $_SESSION['url']; // url for last page visited.
else
$url = "index.php"; // page you want to redirect by default
header("Location: http://abc.com/$url"); ?.
Send the url to return to as a GET paramter in the redirect to the login page:
/login.php?return_url=%2Fcart%2Fproducts.php
In login.php you decode the return_url parameter (/cart/products.php) and send the user there on successful login.
When you redirect to the login page, put the URL that you intercepted (often called a return URL) into a session variable (or some other storage as appropriate). Then, when login is complete you can redirect to this URL.
You sometimes see the return URL in website querystrings during a logon process. For example if you go to docs.google.com when you're not logged into any google account you can see a "continue" value in the querystring as below:
https ://www.google.com/accounts/ServiceLogin?service=writely&passive=1209600&continue=http://docs.google.com/&followup=http://docs.google.com/<mpl=homepage
Keep the
<input type="hidden" name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" />
and get redirurl
value in form post.
if(isset($_REQUEST['redirurl']))
$url = $_REQUEST['redirurl']; // holds url for last page visited.
else
$url = "myprofile.php"; // default page
header("Location:$url");
redirurl
stores the previous Pageurl. keep the session for register and login pages so that it doesn't copy those Page URL.
You can send the user back to the page they came from, using their browser's referer:
header('Location: ' . $_SERVER['HTTP_REFERER']);
精彩评论