Validate if user is logged in and redirect to the page they were trying to access

Asked

Viewed 203 times

0

Hello,

I have the following question. When the user accesses the page: /wallet, there is a validation to know if he is logged in, if he is not, he is redirected to the login page, but after the login he falls on the home page. It would have to redirect to the page: /card, because he tried to access there first?

Follow the code that goes in the header and validate the user:

<?php

  define ( "_WOJO" , true );
  require_once ( "../init.php" );

  if ( ! App :: Auth () -> is_User ())
     Url :: redirect (SITEURL .  '/' );

?> 

1 answer

2


You can get the user’s origin by using "filter_input" with the variable HTTP_REFERER.

In the login validation file, try something like:

$origem = filter_input(INPUT_SERVER, 'HTTP_REFERER');
$destino = "https://dominio.com/login";

if (!is_null($origem)) {
   $destino = $origem; // ex. /carteirinha
}

header('Location: ' . $destino, true, 302);
  • It worked out! Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.