Updated for auth php for education purpose

This commit is contained in:
usami 2025-02-27 11:41:00 +08:00
parent e88bfa5b73
commit 16c549b7cb
2 changed files with 75 additions and 0 deletions

65
auth.php Normal file
View File

@ -0,0 +1,65 @@
<?php
session_start();
$users = [
'admin' => '123456',
'guest' => 'abc123'
];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user = $_POST['username'] ?? '';
$pass = $_POST['password'] ?? '';
if (isset($users[$user]) && $users[$user] === $pass) {
$_SESSION['user'] = $user;
} else {
$error = "登录失败";
}
}
// 处理退出
if (isset($_GET['logout'])) {
unset($_SESSION['user']);
}
?>
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<title>极简登录</title>
<style>
.box { width: 300px; margin: 100px auto; padding: 20px; border: 1px solid #ccc; }
input { margin: 5px 0; padding: 8px; width: 95%; }
button { padding: 8px 20px; background: #007bff; color: white; border: none; }
</style>
</head>
<body>
<?php if (isset($_SESSION['user'])): ?>
<div class="box">
<h2>欢迎 <?php echo $_SESSION['user'] ?>!</h2>
<a href="?logout">退出登录</a>
</div>
<?php else: ?>
<div class="box">
<h2>用户登录</h2>
<?php if (isset($error)) echo "<p style='color:red'>$error</p>" ?>
<form method="POST">
<label>
<input type="text" name="username" placeholder="用户名" required>
</label><br>
<label>
<input type="password" name="password" placeholder="密码" required>
</label><br>
<button type="submit">登录</button>
</form>
<p style="margin-top:15px;color:#666">
测试账号:<br>
admin / 123456<br>
guest / abc123
</p>
</div>
<?php endif; ?>
</body>
</html>

10
login.html Normal file
View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>