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";
?>