Logged in user can enter the login page

Asked

Viewed 95 times

0

when the user logs in he creates sessoes and goes to the panel, but he still has access to the login page. how to send it back to the.php panel when it tries to go to the login page?

sessoes:

$user = $_POST['usuario'];
$_SESSION['user'] = $user;
$_SESSION['status'] = 'LOGADO';
$_SESSION['usuario'] = $busca ['usuario'];
$_SESSION['senha'] = $busca ['senha'];
  • Simply do an if by checking the session. If you have opened or set a specific value, redirect the.php panel with script or header.

  • ah . -. mds thanks kkk worked if($_SESSION['status'] = 'LOGGED in' ) { header('Location: panel.php'); }

  • @Cryptorwings remembering that if you use $_SESSION['status'] = 'LOGGED in' it will always be true, because the comparison operator is == and not =

  • 1

    I don’t understand this custom that I see enough in PHP to keep giving redirect. Isn’t it a lot simpler to include an "if logged in include( dashboard ) Else include ( login form)? It was just an example, there are a thousand more interesting ways that redirect, which still allow the user to continue the session of the page they stopped (although the redirect is great to exchange a POST for a GET, but then the purpose is already different)

  • 1

    And, on the other hand, what’s wrong with having access to the login? It might even be useful if he wants to log in to another account. Nothing that an "if logged in echo 'you are already logged in as Jose. click here to go to the panel, or use the fields below to log in to another account'.

  • @Bacco I use this way I said, with include. As was by cell phone, I could not post the most complete answer. By the way, I would like to know other ways as you said. Help us and

Show 1 more comment

1 answer

1


2 examples:

Option 1

(will have to change its structure)

You will give the include as logged or not:

if(isset($_SESSION['status'])) {
   include_once 'painel.php';
} else {
   include_once 'login.php;
}

Option 2

(in case you do not allow the login page, and do not change its structure)

Check the login page to see if there is a session, if yes, direct to the content page:

if (!isset($_SESSION['status'])) header('Location: painel.php');

Browser other questions tagged

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