Clean Code

This commit is contained in:
usami 2025-02-27 11:43:25 +08:00
parent 16c549b7cb
commit 309efa4fd2

View File

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