Navruk Navruk - 3 months ago 44x
PHP Question

How to redirect to the same page after 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.


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';
header('Location: ' . $destURL);