I have a cart products page, if a person clicks on a product add to cart button 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With