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?
– user6026
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.
– Lukaz11
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?
– Tiago Boeing
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.
– Lukaz11