cookie实现用户登录、注册、验证
1.用户登录页面
1.1html代码
<?php
//判断是否已经登录
if (isset($_COOKIE['user']))
exit('<script>alert("请不要重复登录");location.href="index.php"</script>');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../css/style.css">
<title>用户登录</title>
</head>
<body>
<h3>用户登录</h3>
<form action="handle.php?action=login" method="post">
<div>
<label for="email">邮箱:</label>
<input type="email" name="email" id="email" placeholder="email@php.cn" autofocus required>
</div>
<div>
<label for="password">密码:</label>
<input type="password" name="password" id="password" placeholder="密码不少于6位" required>
</div>
<div>
<button>点击登录</button>
</div>
</form>
<a href="register.php">还没有账号,注册一个吧</a>
</body>
</html>
1.2 css代码
body {
display: flex;
flex-direction: column;
text-align: center;
color: #555;
font-weight: 300;
}
body h3 {
font-weight: 300;
font-size: 20px;
margin-bottom: 10px;
}
body form {
width: 250px;
padding: 20px;
box-sizing: border-box;
margin: auto;
border-radius: 5px;
box-shadow: 0 0 5px #aaa;
}
body form > div {
height: 36px;
display: flex;
justify-content: space-between;
align-items: center;
}
body form div > label {
width: 50px;
}
body form div:last-of-type {
display: flex;
justify-content: center;
}
body form input:hover {
box-shadow: 0 0 5px #aaa;
}
body form button {
flex: auto;
height: 30px;
background-color: green;
color: white;
border: none;
outline: none;
}
body form button:hover {
background-color: lightcoral;
cursor: pointer;
box-shadow: 0 0 5px #aaa;
}
body a {
color: #888;
text-decoration: none;
margin-top: 15px;
}
body a:hover {
color: lightcoral;
font-weight: bold;
}
2.用户注册
2.1html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../css/style.css">
<title>用户注册</title>
</head>
<body>
<h3>用户注册</h3>
<form action="handle.php?action=register" method="post" onsubmit="return compare()">
<div>
<label for="name">昵称:</label>
<input type="text" name="name" id="name" placeholder="不少于3个字符" required autofocus>
</div>
<div>
<label for="email">邮箱:</label>
<input type="email" name="email" id="email" placeholder="demo@php.cn" required>
</div>
<div>
<label for="p1">密码:</label>
<input type="password" name="p1" id="p1" placeholder="密码不少6个字符" required>
</div>
<div>
<label for="p2">重复:</label></label>
<input type="password" name="p2" id="p2" placeholder="必须和上面的密码一样" required>
</div>
<div>
<button>提交</button><span id="tips" style="color:red"></span>
</div>
</form>
<a href="login.php">我有账号,直接登录</a>
<script>
//严重二次密码是否相等?
function compare() {
if (document.forms[0].p1.value.trim() !== document.forms[0].p2.value.trim()) {
document.querySelector('#tips').innerText = '二次密码不相等';
return false;
}
}
</script>
</body>
</html>
2.2 css代码
同上面的用户登录
3 登录成功
3.1用户首页
<?php
if (isset($_COOKIE['user'])) $user = unserialize($_COOKIE['user']);
// var_dump($user);
// die;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>首页</title>
</head>
<body>
<nav>
<a href="index.php">我的论坛</a>
<?php if (isset($user)) : ?>
<a href="" id="logout"><span style="color:red"><?php echo $user['name'] ?> </span> 退出</a>
<?php else : ?>
<a href="login.php">登录</a>
<?php endif; ?>
</nav>
<script>
//为按钮创建事件监听器
document.querySelector('#logout').addEventListener('click', function(event) {
if (confirm('是否退出')) {
//禁用默认行为,其实就是禁用原<a>标签的点击跳转行为,使用事件的自定义方法处理
event.preventDefault();
//跳转到退出事件处理器
window.location.assign('handle.php?action=logout');
}
});
</script>
</body>
</html>
3.1 css样式
nav {
height: 40px;
background-color: deepskyblue;
padding: 0 20px;
display: flex;
justify-content: space-between;
align-items: center;
}
nav > a {
color: white;
text-decoration: none;
}
4 控制部分
<?php
//查询用户表中的数据
$pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'root');
$stmt = $pdo->prepare("select * from users");
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
//处理用户登录与注册
$action = $_GET['action'];
switch (strtolower($action)) {
case 'login':
//判断请求是否合法
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//获取需要验证的数据
$email = $_POST['email'];
$password = sha1($_POST['password']);
//使用array_filter来验证数据库中是否存在此用户
$results = array_filter($users, function ($user) use ($email, $password) {
return $user['email'] === $email && $user['password'] === $password;
});
if (count($results) == 1) {
setcookie('user', serialize(array_pop($results)));
exit('<script>alert("验证通过");location.href="index.php"</script>');
} else {
exit('<script>alert("邮箱或密码错误,或者还没有帐号");location.href="login.php"</script>');
}
} else {
die('用户类型错误');
}
case 'logout':
if (isset($_COOKIE['user'])) {
setcookie('user', null, time() - 1);
exit('<script>alert("退出成功");location.assign("index.php")</script>');
}
break;
case 'register':
//1.获取到新用户注册数据
$name = $_POST['name'];
$email = $_POST['email'];
$password = sha1($_POST['p1']);
$register_time = time();
//2.将新用户插入到表中
$sql = "INSERT `users` SET `name`='{$name}',`email`='{$email}',`password`='{$password}',`register_time`={$register_time}";
// var_dump($sql);
// exit;
$stmt = $pdo->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() === 1) exit('<script>alert("注册成功");location.assign("login.php")</script>');
else exit('<script>alert("注册失败");location.assign("register.php")</script>');
break;
default:
exit('未定义操作');
}
总结:用户登录注册通过把控制处理代码统一写在一个模块,这个就是MVC中的控制器,通过控制来处理这些逻辑,用户是看不到这个部分代码的
session实现只需要把用到cookie的部分替换成session,然后在代码前面加上session_start()就可以实现了