DEMO:
login.php:
Codice PHP:
<?php
session_start();
if(isset($_SESSION['userid']) && $_SESSION['userid'])
{
header('Location: home.php');
exit;
}
$loginError = false;
if(isset($_POST['login']))
{
$username = $_POST['username'];
$password = $_POST['password'];
//autenticazione fake, usare quella che ritieni opportuna
if($username == 'test' && $password == 'test')
{
$_SESSION['userid'] = $username;
header('Location: home.php');
exit;
}else{
$loginError = true;
}
}
?>
<html>
<head><title>Login</title></head>
<body>
<?php if($loginError): ?>
<h3>Username or password invalid</h3>
<?php endif; ?>
<form action="login.php" method="post">
<div>username: <input type="textbox" name="username" /></div>
<div>password: <input type="password" name="password" /></div>
<input type="submit" name="login" />
</form>
</body>
</html>
home.php:
Codice PHP:
<?php
session_start();
if(!isset($_SESSION['userid']) || !$_SESSION['userid'])
{
header('Location: login.php');
exit;
}
$username = $_SESSION['userid'];
?>
<html>
<head>
<title>Homepage</title>
</head>
<body>
Hello <?php echo $username; ?>
</body>
</html>
se li metti in una cartella (e utilizzi php >= 5.4 ), puoi lanciare il comando:
codice:
php -S localhost:8000
e se nel browser vai in http://localhost:8000/home.php dovresti vedere che funziona correttamente