Appearance
登录模板
登录表单
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.login-container {
background-color: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
}
h2 {
text-align: center;
margin-bottom: 20px;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 3px;
box-sizing: border-box;
}
input[type="submit"] {
width: 100%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.error {
color: red;
margin-bottom: 15px;
}
.success {
color: green;
margin-bottom: 15px;
}
</style>
</head>
<body>
<div class="login-container">
<h2>用户登录</h2>
<?php if (isset($error)) { echo '<div class="error">' . $error . '</div>'; } ?>
<?php if (isset($success)) { echo '<div class="success">' . $success . '</div>'; } ?>
<form method="post" action="login.php">
<div class="form-group">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
</div>
<div class="form-group">
<input type="checkbox" id="remember" name="remember">
<label for="remember">记住我</label>
</div>
<input type="submit" value="登录">
</form>
</div>
</body>
</html>登录处理
php
<?php
// 启动会话
session_start();
// 初始化变量
$error = '';
$success = '';
// 检查是否提交表单
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 接收表单数据
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
$remember = isset($_POST['remember']) ? true : false;
// 验证数据
if (empty($username)) {
$error = '用户名不能为空';
} elseif (empty($password)) {
$error = '密码不能为空';
} else {
// 模拟数据库验证
// 实际应用中,应该从数据库中查询用户信息并验证密码
$valid_username = 'admin';
$valid_password = 'password';
if ($username == $valid_username && $password == $valid_password) {
// 登录成功,设置会话
$_SESSION['username'] = $username;
$_SESSION['logged_in'] = true;
// 记住我功能
if ($remember) {
// 生成令牌
$token = bin2hex(random_bytes(32));
// 存储令牌到数据库(实际应用中)
// 这里只是模拟
setcookie('remember_token', $token, time() + 30 * 24 * 3600, '/');
}
// 跳转到首页
header('Location: index.php');
exit;
} else {
$error = '用户名或密码错误';
}
}
}
// 显示登录表单
include 'login_form.php';
?>登录状态检查
php
<?php
// 启动会话
session_start();
// 检查登录状态
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
// 检查记住我
if (isset($_COOKIE['remember_token'])) {
// 验证令牌(实际应用中)
// 这里只是模拟
$_SESSION['username'] = 'admin';
$_SESSION['logged_in'] = true;
} else {
// 未登录,跳转到登录页
header('Location: login.php');
exit;
}
}
// 登录成功,显示首页
echo "欢迎回来," . $_SESSION['username'] . "!";
echo '<br><a href="logout.php">退出登录</a>';
?>退出登录
php
<?php
// 启动会话
session_start();
// 清除会话
$_SESSION = array();
// 清除会话 cookie
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// 销毁会话
session_destroy();
// 清除记住我 cookie
if (isset($_COOKIE['remember_token'])) {
setcookie('remember_token', '', time() - 42000, '/');
}
// 跳转到登录页
header('Location: login.php');
exit;
?>注册表单
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.register-container {
background-color: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
}
h2 {
text-align: center;
margin-bottom: 20px;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"],
input[type="email"],
input[type="password"] {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 3px;
box-sizing: border-box;
}
input[type="submit"] {
width: 100%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.error {
color: red;
margin-bottom: 15px;
}
.success {
color: green;
margin-bottom: 15px;
}
</style>
</head>
<body>
<div class="register-container">
<h2>用户注册</h2>
<?php if (isset($error)) { echo '<div class="error">' . $error . '</div>'; } ?>
<?php if (isset($success)) { echo '<div class="success">' . $success . '</div>'; } ?>
<form method="post" action="register.php">
<div class="form-group">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
</div>
<div class="form-group">
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
</div>
<div class="form-group">
<label for="confirm_password">确认密码:</label>
<input type="password" id="confirm_password" name="confirm_password" required>
</div>
<input type="submit" value="注册">
</form>
</div>
</body>
</html>注册处理
php
<?php
// 启动会话
session_start();
// 初始化变量
$error = '';
$success = '';
// 检查是否提交表单
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 接收表单数据
$username = isset($_POST['username']) ? $_POST['username'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
$confirm_password = isset($_POST['confirm_password']) ? $_POST['confirm_password'] : '';
// 验证数据
if (empty($username)) {
$error = '用户名不能为空';
} elseif (empty($email)) {
$error = '邮箱不能为空';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = '邮箱格式不正确';
} elseif (empty($password)) {
$error = '密码不能为空';
} elseif (strlen($password) < 6) {
$error = '密码长度不能少于 6 位';
} elseif ($password != $confirm_password) {
$error = '两次输入的密码不一致';
} else {
// 模拟数据库注册
// 实际应用中,应该将用户信息插入数据库
// 这里只是模拟
$success = '注册成功,请登录';
// 跳转到登录页
header('Location: login.php?success=注册成功,请登录');
exit;
}
}
// 显示注册表单
include 'register_form.php';
?>总结
本文提供了 PHP 中用户登录和注册的基本模板,包括登录表单、登录处理、登录状态检查、退出登录、注册表单和注册处理等功能。这些模板可以作为开发中的参考,帮助你快速实现用户认证功能。
在使用这些模板时,应该注意以下几点:
- 对输入数据进行验证和过滤
- 使用密码哈希处理用户密码
- 实现记住我功能
- 正确处理会话和 cookie
- 遵循最佳实践和编码规范
通过使用这些模板,你可以更高效地开发 PHP 应用程序,提高代码质量和安全性。
