77 lines
No EOL
2.4 KiB
PHP
77 lines
No EOL
2.4 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
// Database connection details
|
|
$host = "192.168.1.20";
|
|
$database = "orologi";
|
|
$user = "pdo";
|
|
$password = "";
|
|
|
|
// Initialize error message
|
|
$error = '';
|
|
|
|
// Check if the form is submitted
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
try {
|
|
// Establish a connection to the database
|
|
$connection = new PDO("mysql:host=$host;dbname=$database", $user, $password);
|
|
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
// Get the username and password from the form
|
|
$username = $_POST['username'];
|
|
$password = $_POST['password'];
|
|
|
|
// Prepare the SQL statement to fetch the user
|
|
$stmt = $connection->prepare("SELECT * FROM utenti WHERE user = :username");
|
|
$stmt->execute(['username' => $username]);
|
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
// Verify the password
|
|
if ($user && $user['password'] === $password) {
|
|
// Password is correct, start a new session
|
|
$_SESSION['user_id'] = $user['idut'];
|
|
$_SESSION['username'] = $user['user'];
|
|
$_SESSION['livello'] = $user['livello'];
|
|
$_SESSION["login"] = true;
|
|
|
|
// Redirect to a different page after successful login
|
|
header("Location: index.php");
|
|
exit();
|
|
} else {
|
|
// Invalid credentials
|
|
$error = "Invalid username or password.";
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error = "Error: " . $e->getMessage();
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Login</title>
|
|
<link rel="stylesheet" href="https://unpkg.com/@picocss/pico@2/css/pico.min.css">
|
|
</head>
|
|
<body>
|
|
<main class="container">
|
|
<h1>Login</h1>
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><?php echo $error; ?></div>
|
|
<?php endif; ?>
|
|
<form method="post" action="">
|
|
<label for="username">Username:</label>
|
|
<input <?php if ($error):
|
|
echo "aria-invalid=\"true\""; endif; ?> type="text" id="username" name="username" required>
|
|
|
|
<label for="password">Password:</label>
|
|
<input <?php if ($error):
|
|
echo "aria-invalid=\"true\""; endif; ?> type="password" id="password" name="password" required>
|
|
|
|
<button type="submit">Login</button>
|
|
</form>
|
|
</main>
|
|
</body>
|
|
</html>
|