mirror of
https://github.com/zhang-pu/guestbook.git
synced 2026-12-01 09:18:27 +08:00
88 lines
3.7 KiB
PHP
88 lines
3.7 KiB
PHP
<?php
|
||
/**
|
||
* 管理员登录(加固版):CSRF + 登录限流 + 通用错误提示 + 登录后换 SID
|
||
*/
|
||
declare(strict_types=1);
|
||
require_once __DIR__ . '/config.php';
|
||
|
||
if (!empty($_SESSION['admin_logged_in'])) {
|
||
header('Location: admin.php');
|
||
exit;
|
||
}
|
||
|
||
$error = '';
|
||
if (is_post()) {
|
||
require_csrf();
|
||
$username = get_post('username');
|
||
$password = get_post('password');
|
||
|
||
if ($username === '' || $password === '') {
|
||
$error = '请输入用户名和密码';
|
||
} elseif (!rate_limit('login:' . client_ip(), RATE_LIMIT_LOGIN, RATE_LIMIT_LOGIN_WINDOW)) {
|
||
$error = '尝试次数过多,请 10 分钟后再试';
|
||
log_error('登录限流触发 ip=' . client_ip());
|
||
} else {
|
||
try {
|
||
$db = get_db();
|
||
$st = $db->prepare('SELECT id, password_hash FROM admins WHERE username = ?');
|
||
$st->bindValue(1, $username, SQLITE3_TEXT);
|
||
$row = $st->execute()->fetchArray(SQLITE3_ASSOC);
|
||
if ($row && password_verify($password, (string)$row['password_hash'])) {
|
||
session_regenerate_id(true); // 防会话固定
|
||
$_SESSION['admin_logged_in'] = true;
|
||
$_SESSION['admin_username'] = $username;
|
||
$_SESSION['login_time'] = time();
|
||
header('Location: admin.php');
|
||
exit;
|
||
}
|
||
log_error('登录失败 username=' . $username . ' ip=' . client_ip());
|
||
$error = '用户名或密码错误';
|
||
} catch (Throwable $e) {
|
||
log_error('登录异常: ' . $e->getMessage());
|
||
$error = '登录失败,请稍后再试';
|
||
}
|
||
}
|
||
}
|
||
?>
|
||
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>后台登录 - <?= e(SITE_NAME) ?></title>
|
||
<link rel="stylesheet" href="assets/style.css">
|
||
<style>
|
||
body { background: #f5f5f5; }
|
||
.login-container { max-width: 400px; margin: 100px auto; background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,.1); }
|
||
.login-form .form-group { margin-bottom: 15px; }
|
||
.login-form label { display: block; margin-bottom: 5px; font-weight: bold; }
|
||
.login-form input { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; box-sizing: border-box; }
|
||
.login-form button { width: 100%; padding: 10px; background: #2196f3; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; }
|
||
.login-form button:hover { background: #1976d2; }
|
||
.error { color: #f44336; margin-bottom: 15px; padding: 10px; background: #ffebee; border-radius: 4px; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="login-container">
|
||
<h2 style="text-align:center;">后台登录</h2>
|
||
<?php if (!empty($_GET['timeout'])): ?>
|
||
<div class="error">登录已超时,请重新登录</div>
|
||
<?php endif; ?>
|
||
<?php if ($error !== ''): ?>
|
||
<div class="error"><?= e($error) ?></div>
|
||
<?php endif; ?>
|
||
<form method="POST" action="login.php" class="login-form">
|
||
<?= csrf_field() ?>
|
||
<div class="form-group">
|
||
<label for="username">用户名:</label>
|
||
<input type="text" id="username" name="username" required autocomplete="username" value="<?= e(get_post('username')) ?>">
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="password">密码:</label>
|
||
<input type="password" id="password" name="password" required autocomplete="current-password">
|
||
</div>
|
||
<button type="submit">登录</button>
|
||
</form>
|
||
</div>
|
||
</body>
|
||
</html>
|