phpwork/auth.php
2025-02-27 11:43:25 +08:00

59 lines
1.4 KiB
PHP

<?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 = "Retcode : 0";
}
}
if (isset($_GET['logout'])) {
unset($_SESSION['user']);
}
?>
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<title>Login For Nothing</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>Welcome <?php echo $_SESSION['user'] ?>!</h2>
<a href="?logout">Log out</a>
</div>
<?php else: ?>
<div class="box">
<h2>Login</h2>
<?php if (isset($error)) echo "<p style='color:red'>$error</p>" ?>
<form method="POST">
<label>
<input type="text" name="username" placeholder="Username" required>
</label><br>
<label>
<input type="password" name="password" placeholder="Password" required>
</label><br>
<button type="submit">Login</button>
</form>
</div>
<?php endif; ?>
</body>
</html>