PHP_Esercizio1/form.php
Sergio-Bianchi 5dc2ac7922 First commit
2025-10-17 09:33:37 +02:00

57 lines
1.4 KiB
PHP

<?php
use services\Access;
require_once('services/Access.php');
session_start();
$login_error = null;
if(isset($_SESSION['login_error'])){
$login_error = $_SESSION['login_error'];
unset($_SESSION['login_error']);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
error_log("POST data: " . json_encode($_POST));
$data = $_POST;
if (!empty($data['username']) && !empty($data['password'])) {
$result = Access::login($data['username'], $data['password']);
if($result && $result->num_rows > 0) {
$_SESSION['username'] = $data['username'];
$_SESSION['password'] = $data['password'];
header('Location: http://localhost:8080/index.php/');
} else {
$login_error = "Username o password errati";
$_SESSION['login_error'] = $login_error;
header('Location: http://localhost:8080/form.php/');
}
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>LOGIN</h1>
<form action="" method="post">
<label>
<input type="text" placeholder="username" name="username">
</label>
<label>
<input type="password" placeholder="password" name="password">
</label>
<button type="submit">Entra</button>
</form>
<?php
if($login_error) {
echo $login_error;
}
?>
</body>
</html>