编程题 编写一个登入页面和登入服务,要求如下: 1.用户可以选择7天内记住密码 2.用户可以设置取消记住密码 3.用户可以设置自动登入 4.用户可以取消自动登入
时间: 2024-05-15 09:14:52 浏览: 57
登录页面记住用户名密码
3星 · 编辑精心推荐
以下是一个简单的登入页面和登入服务的代码,使用了HTML、CSS、JavaScript和PHP:
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="script.js"></script>
</head>
<body>
<div class="container">
<h1>Login Page</h1>
<form action="login.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<br>
<input type="checkbox" id="remember" name="remember">
<label for="remember">Remember me for 7 days</label>
<br>
<input type="checkbox" id="autologin" name="autologin">
<label for="autologin">Auto login</label>
<br>
<input type="submit" value="Login">
</form>
</div>
</body>
</html>
```
CSS代码:
```
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
}
.container {
width: 400px;
margin: 50px auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0px 0px 10px #aaa;
}
h1 {
text-align: center;
margin-top: 0px;
}
form {
text-align: center;
}
label {
display: inline-block;
width: 80px;
text-align: right;
margin-right: 10px;
}
input[type="text"], input[type="password"], input[type="checkbox"], input[type="submit"] {
padding: 5px;
border-radius: 5px;
border: 1px solid #ccc;
margin: 5px;
}
input[type="submit"] {
background-color: #4CAF50;
color: #fff;
cursor: pointer;
}
```
JavaScript代码:
```
window.onload = function() {
var remember = document.getElementById("remember");
var autologin = document.getElementById("autologin");
if (localStorage.getItem("remember") === "true") {
remember.checked = true;
document.getElementById("username").value = localStorage.getItem("username");
document.getElementById("password").value = localStorage.getItem("password");
}
if (localStorage.getItem("autologin") === "true") {
autologin.checked = true;
document.forms[0].submit();
}
remember.onchange = function() {
if (this.checked) {
localStorage.setItem("remember", "true");
localStorage.setItem("username", document.getElementById("username").value);
localStorage.setItem("password", document.getElementById("password").value);
} else {
localStorage.removeItem("remember");
localStorage.removeItem("username");
localStorage.removeItem("password");
}
};
autologin.onchange = function() {
if (this.checked) {
localStorage.setItem("autologin", "true");
} else {
localStorage.removeItem("autologin");
}
};
};
```
PHP代码:
```
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
// Check username and password with database here
if (isset($_POST["remember"])) {
setcookie("username", $username, time() + 7*24*60*60);
setcookie("password", $password, time() + 7*24*60*60);
} else {
setcookie("username", "", time() - 3600);
setcookie("password", "", time() - 3600);
}
if (isset($_POST["autologin"])) {
session_start();
$_SESSION["username"] = $username;
}
// Redirect to home page after login
header("Location: home.php");
exit();
}
?>
```
说明:
1. HTML代码中包含一个表单,用于输入用户名、密码和勾选框,以及一个JavaScript脚本,用于处理勾选框的状态和保存用户名和密码。
2. CSS代码用于美化页面。
3. JavaScript代码在页面加载时检查本地存储中是否保存了用户名和密码,如果是则将其填充到表单中,并根据勾选框的状态保存或删除本地存储。
4. PHP代码用于处理表单提交,验证用户名和密码,并根据勾选框的状态设置或删除cookie和会话变量。最后重定向到主页。
阅读全文