0

I am having this problem, when someone enters the page that requires login, so the page will redirect the user to the login page, after the user completes login step, he/she should be redirected to previous page from he/she was redirected to login page.

Here is a condition on all login required pages:

<?php
session_start();
if(!isset($_SESSION['email'])) {
    header("Location: login.php);
}
?>

What can I add to redirect the user to previous page after successful login?

  • Already solutions are there - https://stackoverflow.com/questions/5285031/back-to-previous-page-with-header-location-in-php. – Rajkumar R Jun 04 '21 at 05:15
  • @RajkumarR - That's not a good solution since it depends on the HTTP_REFERER header, which is know for not being reliable. Not only can it be manipulated/overwritten by the client, but for privacy reasons, some routers/firewalls/clients don't always send it with the request. A better solution would be to store the current page URL in a session before redirecting to the login page. Then on a successful login, redirect the user back to the URL stored in the session (and remove it from the session). – M. Eriksson Jun 04 '21 at 05:51
  • 1
    Thanks @Magnus Eriksson I have no problem now. My page is now going to the correct page. –  Jun 05 '21 at 06:30

0 Answers0