How to redirect to the same page after login

I have a product page with carts, if a person clicks the button to add a product to the cart, they will be redirected to the login page.

After a successful login, I need to send the user back to the same product page.

+5
source share
6 answers

A simple solution would be to store the return URL in a session variable before you go to the login page. The login page will check for the presence of a session variable, and then disable it before using header redirection to return the user to the specified URL.

For example, on the login page you will use:

// Successfully logged in...
$destURL = $_SESSION['kickurl'] ? $_SESSION['kickurl'] : '/index.php';
unset($_SESSION['kickurl']);
header('Location: ' . $destURL);
exit();
+11

URL- : : <?php $_SESSION['url'] = $_SERVER['REQUEST_URI']; ?>

: <?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"); ?.

+3

URL- GET :

/login.php?return_url=%2Fcart%2Fproducts.php

login.php return_url (/cart/products.php) .

+1

URL-, ( URL- ), ( - ). , , URL.

URL- - . , docs.google.com, - google, "" :

https://www.google.com/accounts/ServiceLogin? service = writely & passive = 1209600 & continue = http://docs.google.com/& followup = http://docs.google.com/&amp; ltmpl =

+1

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

redirurl .

if(isset($_REQUEST['redirurl']))
   $url = $_REQUEST['redirurl']; // holds url for last page visited.
else
   $url = "myprofile.php"; // default page

header("Location:$url");

redirurl . , URL .

0

You can send the user back to the page from which they came using your browser referrer:

header('Location: ' . $_SERVER['HTTP_REFERER']);

-1
source

All Articles