How do I redirect the login page to the control panel when I am logged in?

Asked

Viewed 500 times

1

With this code I redirect from the control panel to the login page visitors who are not logged in.

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

How could I apply the same process to the Login page but when I am already logged in?

Login page

<?php
$page = "Login";
include "header.php";

$user_error = '';
$pass_error = '';
$login_error = '';

if(isset($_POST["login"])){

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

if(empty($username)){
    $user_error = 'Please insert a username';
}
if(empty($password)){
    $pass_error = 'Please insert a password';
}
else{
    $login_check = mysql_query("SELECT * FROM `database`.`user` WHERE `username` = '".$username."' AND `password` = '".$password."'");
    if(mysql_num_rows($login_check) == 0){
        $login_error = 'Wrong username and password combination';
    }
}
 }
 if(empty($user_error)&& empty($pass_error)&& empty($login_error)&& isset($_POST['login'])){

$login_check = mysql_query("SELECT * FROM `database`.`user` WHERE `username` = '".$username."' and password = '".$password."'") or die(mysql_error());

if(mysql_num_rows($login_check) == 1){

    session_start();
    $_SESSION['username'] = $username;
    header("Location: control-painel.php");
}
 }
 else{
$user_error = empty($user_error)?'' : htmlEntities($user_error);
$pass_error = empty($pass_error)?'' : htmlEntities($pass_error);
$login_error = empty($login_error)?'' : htmlEntities($login_error);
?>
<?php
}
include "footer.php";
?>      
  • If you log in it redirects to some page if Session is already with user?

  • At the moment you log in you are automatically redirected to the control panel, but you can still access Login. How can someone log in if they are already logged in, right? That’s why I wanted to restrict Login during the session.

  • What if you went the other way? In the case of the first code you say, if there is no user session send it to the login page, if you put an ELSE and send it to index.php (panel) it would not solve?

  • It does not work because placing an Else{header (Location: control-panel;)} inside the control panel will create a loop and the page will not load.

1 answer

1


In the code of login.php put so in the first lines !!!

<?php
   session_start();
   if (isset($_SESSION['username']))
   {                  
       header("location: admin.php"); 
   }       
?>

In the other places

<?php
   session_start();
   if (!isset($_SESSION['username'])){
      header("location: login.php");
   }
?>
  • I keep going to the Login page, this $_SERVER["SCRIPT_NAME"] has to change its name?

  • No it doesn’t need to change anything! I can see your code on the login page?

  • @Lukaz11 I edited, take a look !!!

  • Recalling that the isset does not validate if the variable is empty, only if it exists. The ideal would be to use an Empty as well.

Browser other questions tagged

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