how to send the user to the home page if they are not logged in using sessions

0

I'm doing a simple login, where the user puts the data and takes it to the panel, but what I want to do is that if another user wants to enter that panel he can not access if he is not logged in.

If you know, please help me.

validate:

<?php

$miuser = "root";
$mipass = "1234";

if(isset($_POST['login'])) {
	$usuario = $_POST['usuario'];
	$pass = $_POST['password'];
	if ($usuario == $miuser and $pass == $mipass ) {
        if (isset($_POST['remember'])) {
        	setcookie('usuario', $usuario, time()+60*60*7);
        	setcookie('passowrd', $pass, time()+60*60*7);
        } 
        session_start();
        	$_SESSION['usuario'] = $usuario;
        	header("location: feed.php");
    } else {
    	echo "usuario o clave son incorrectos";
    }
} else {
	header("location: login.php");
}

?>

user panel:

<?php 
session_start();
echo "Bienvenido" . $_SESSION['usuario'];
echo "<a href='salir.php'>salir</a>"
?>

salir.php

<?php

session_start();
session_destroy();
echo "has salido correctamente";
?>
    
asked by danielmeza 21.11.2018 в 22:19
source

1 answer

2

add this to your panel page, at the top in a php script

session_start();
if( !isset( $_SESSION["usuario"] ) ){
    header("Location: login.php");
}

   // codigo de tu panel

validates if the session variable exists I hope I helped you

    
answered by 21.11.2018 / 22:30
source