classe IV E SIA - Esercizio corretto con il login +cookie
Login.php
<html>
<head>
<title>Login Area</title>
</head><body>
<?php
if(isset($_GET['error_login']))
{
echo "Error ,please compile correctly the form!";
}
?>
<form method="post" action="login3.php">
<table>
<tr>
<td>Username</td>
</tr>
<tr>
<td><input type="textbox" name="user" id="user" /></td>
</tr>
<tr>
<td>Password</td>
</tr>
<tr>
<td><input type="text" name="pass" id="pass"/></td>
</tr>
<tr>
<td><input type="submit" value="Log In" /></td>
</tr>
</table>
</form>
</body>
</html>
Login2.php
<?php
session_start();
$user="user";
$pass="password";
$page_to="page.php";
if(isset($_POST['user']) && isset($_POST['pass']))
{
if( $_POST['user']==$user && $_POST['pass']==$pass)
{
$_SESSION['isLogged']="true";
header("Location:".$page_to);
}
else
{
header("Location:login.php?error_login=1");
}
}
else
{
header("Location:login.php?error_login=1");
}
?>
Login3.php
<?php
session_start();
$utenti[0]["user"]="user1";
$utenti[0]["pass"]="pass1";
$utenti[1]["user"]="user2";
$utenti[1]["pass"]="pass2";
$utenti[2]["user"]="user3";
$utenti[2]["pass"]="pass3";
$page_to="page.php";
$isLogged=false;
if(isset($_POST['user']) && isset($_POST['pass']))
{
for($i=0;$i<count($utenti);$i++) {
if( $_POST['user']==$utenti[$i]["user"] && $_POST['pass']==$utenti[$i]["pass"] )
{
$isLogged=true;
}
}
if($isLogged)
{
$_SESSION['isLogged']="true";
header("Location:".$page_to);
}
else
{
header("Location:login.php?error_login=1");
}
}
else {
header("Location:login.php?error_login=1");
}
?>
page.php
<?php
session_start();
if(!isset($_SESSION['isLogged']))
{
header("Location:login.php?error_login=1");
}
else {
if($_SESSION['isLogged']!="true")
{
header("Location:login.php?error_login=1");
}
}
?>
<html>
<head>
</head>
<body>
<div>
Utente Loggato!
</div>
</body>
</html>
Commenti
Posta un commento