Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect to the same page after login

Tags:

php

login

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.

like image 469
Navruk Avatar asked Jan 27 '11 12:01

Navruk


3 Answers

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();
like image 55
John Parker Avatar answered Sep 26 '22 02:09

John Parker


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

like image 38
Sushil Kandola Avatar answered Sep 24 '22 02:09

Sushil Kandola


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.

like image 38
Björn Lindqvist Avatar answered Sep 27 '22 02:09

Björn Lindqvist