-1

Currently i am working on a ecommerce project in php. I face a problem.
I want to restrict users from accessing add cart page before login. so if the users is not logged in when user clicked on add to cart , he will redirect to login page. Here the problem comes. after login i want users to redirect to cart page. not the dashboard page. and if he login from default way[means not from cart page] he will go to dashboard.

so how can i redirect users to cart page when he entered login page from cart page and also if he is not accessing login page from cart page he will go to dashboard.

sajibzzz
  • 13
  • 3
  • You might be able to accomplish this using the referrer, or setup a preconfigured redirect for each of these login forms. – Adam Rodriguez Dec 07 '18 at 20:33
  • 1
    "I want to restrict users from accessing add cart page before login" Just FYI, I (and I'm guessing many other people) would never shop on a site with such a behavior. – Patrick Q Dec 07 '18 at 20:34

1 Answers1

0

Lots of websites passes a redirect url in the GET query string to the login page like:

$current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
header('Location: login.php?return='.urlencode($current_url));

Then on login redirect:

$url = $_GET['return'] ? urldecode($_GET['return']) : "dashboard.php";
header("Location: $url");

You can also save the url to the session if you to want to pass it in the url.

Kubwimana Adrien
  • 2,463
  • 2
  • 8
  • 11