cookie login stuff
This commit is contained in:
parent
96fe9915d9
commit
b69a4de7ab
29
auth_cookie.php
Normal file
29
auth_cookie.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
session_start();
|
||||
if (isset($_COOKIE['login_time']) && isset($_COOKIE['username'])) {
|
||||
header("Location: welcome.php");
|
||||
exit();
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="">
|
||||
<head>
|
||||
<title>Login</title>
|
||||
</head>
|
||||
<body>
|
||||
<?php if (isset($_GET['error'])): ?>
|
||||
<p style="color: red;">
|
||||
<?php
|
||||
if ($_GET['error'] === 'invalid') echo "Invalid credentials!";
|
||||
if ($_GET['error'] === 'expired') echo "Session expired!";
|
||||
?>
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
|
||||
<form action="do_login.php" method="post">
|
||||
Username: <input type="text" name="username"><br>
|
||||
Password: <input type="password" name="password"><br>
|
||||
<input type="submit" value="Login">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
19
do_login.php
Normal file
19
do_login.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
session_start();
|
||||
// 简单验证(实际应使用数据库和密码哈希)
|
||||
$valid_username = 'admin';
|
||||
$valid_password = 'password';
|
||||
|
||||
$username = $_POST['username'] ?? '';
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
if ($username === $valid_username && $password === $valid_password) {
|
||||
$login_time = time();
|
||||
// 设置Cookie(60秒有效期)
|
||||
setcookie('username', $username, $login_time + 60, '/');
|
||||
setcookie('login_time', $login_time, $login_time + 60, '/');
|
||||
header("Location: welcome.php");
|
||||
} else {
|
||||
header("Location: auth_cookie.php?error=invalid");
|
||||
}
|
||||
exit();
|
8
logout.php
Normal file
8
logout.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
session_start();
|
||||
// 清除Cookie
|
||||
setcookie('username', '', time() - 3600, '/');
|
||||
setcookie('login_time', '', time() - 3600, '/');
|
||||
header("Location: login.php");
|
||||
exit();
|
||||
?>
|
36
welcome.php
Normal file
36
welcome.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
session_start();
|
||||
// 验证Cookie
|
||||
if (!isset($_COOKIE['login_time']) || !isset($_COOKIE['username'])) {
|
||||
header("Location: login.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
$current_time = time();
|
||||
$login_time = (int)$_COOKIE['login_time'];
|
||||
$remaining = 60 - ($current_time - $login_time);
|
||||
|
||||
if ($remaining <= 0) {
|
||||
// 清除过期Cookie
|
||||
setcookie('username', '', time() - 3600, '/');
|
||||
setcookie('login_time', '', time() - 3600, '/');
|
||||
header("Location: login.php?error=expired");
|
||||
exit();
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Welcome</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>Welcome <?php echo htmlspecialchars($_COOKIE['username']); ?>!</h2>
|
||||
<p>Session expires in: <?php echo $remaining; ?> seconds</p>
|
||||
<a href="logout.php">Logout</a>
|
||||
|
||||
<script>
|
||||
// 自动刷新剩余时间
|
||||
setTimeout(() => location.reload(), 1000);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user