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

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 can I apply the same process to the Login page but when I'm 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";
?>      
    
asked by anonymous 24.05.2014 / 17:48

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 others place

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