Login umgestellt auf tokem je Gerät
This commit is contained in:
45
login.php
45
login.php
@@ -2,12 +2,12 @@
|
||||
session_start();
|
||||
include('inc/db.php');
|
||||
|
||||
$error = '';
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$username = $_POST['username'];
|
||||
$password = $_POST['password'];
|
||||
|
||||
// 1. Prepared Statement vorbereiten
|
||||
$stmt = mysqli_prepare($conn, "SELECT id, username, password, email, role FROM users WHERE username = ?");
|
||||
if ($stmt) {
|
||||
mysqli_stmt_bind_param($stmt, "s", $username);
|
||||
@@ -23,18 +23,19 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$_SESSION['email'] = $user['email'];
|
||||
$_SESSION['role'] = $user['role'];
|
||||
|
||||
// Token generieren und in der Datenbank speichern
|
||||
// Neuen Login-Token für die geräteübergreifende Anmeldung erstellen
|
||||
$token = bin2hex(random_bytes(32));
|
||||
$update_stmt = mysqli_prepare($conn, "UPDATE users SET login_token = ? WHERE id = ?");
|
||||
if ($update_stmt) {
|
||||
mysqli_stmt_bind_param($update_stmt, "si", $token, $user['id']);
|
||||
mysqli_stmt_execute($update_stmt);
|
||||
mysqli_stmt_close($update_stmt);
|
||||
}
|
||||
$expires_at = date('Y-m-d H:i:s', strtotime('+30 days'));
|
||||
|
||||
// Cookies setzen, die 30 Tage gültig sind
|
||||
setcookie('auth_token', $token, time() + (86400 * 30), "/");
|
||||
setcookie('user_id', $user['id'], time() + (86400 * 30), "/");
|
||||
// Token in der neuen `login_tokens` Tabelle speichern
|
||||
$sql_token = "INSERT INTO login_tokens (user_id, token, expires_at) VALUES (?, ?, ?)";
|
||||
$stmt_token = mysqli_prepare($conn, $sql_token);
|
||||
mysqli_stmt_bind_param($stmt_token, "iss", $user['id'], $token, $expires_at);
|
||||
mysqli_stmt_execute($stmt_token);
|
||||
mysqli_stmt_close($stmt_token);
|
||||
|
||||
// Cookie mit dem Token setzen
|
||||
setcookie('remember_token', $token, time() + (86400 * 30), "/");
|
||||
|
||||
header("Location: index.php");
|
||||
exit();
|
||||
@@ -42,11 +43,9 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$error = "Login fehlgeschlagen.";
|
||||
}
|
||||
} else {
|
||||
// Fehler beim Vorbereiten des Statements
|
||||
$error = "Datenbankfehler.";
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
@@ -56,31 +55,22 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>DoMiLi – Login</title>
|
||||
<!-- Bootstrap CSS -->
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<!-- Google Fonts Icons -->
|
||||
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined" rel="stylesheet">
|
||||
<!-- Custom styles -->
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div class="container d-flex justify-content-center align-items-start min-vh-100 py-4 pt-5">
|
||||
<div class="container d-flex justify-content-center align-items-start py-4 pt-5">
|
||||
<div class="card bg-light shadow w-100" style="max-width: 400px;">
|
||||
<div class="card-body">
|
||||
<h4 class="card-title text-center mb-4 fs-3">DoMiLi Login</h4>
|
||||
|
||||
<?php if (isset($error)) {
|
||||
?>
|
||||
<?php if ($error) { ?>
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<?php echo $error; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
|
||||
<?php } ?>
|
||||
<form method="post" action="">
|
||||
<div class="mb-3">
|
||||
<label for="username" class="form-label">Benutzername</label>
|
||||
@@ -97,6 +87,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php include('inc/footer.php'); ?>
|
||||
</body>
|
||||
|
||||
|
||||
<?php include('inc/footer.php'); ?>
|
||||
</html>
|
||||
Reference in New Issue
Block a user