登录与验证,新用户注册功能(cookie 与 session)
1.连接数据库用户表
$pdo = new PDO('mysql:host=localhost;dbname=php', 'root', 'root123456');
// var_dump($pdo);
// die;
$sql = 'SELECT * FROM `user` ';
$stmt = $pdo->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
2.处理用户注册登录脚本(COOKIE)
//处理用户登录和注册
$action = $_GET['action'];
switch (strtolower($action)) {
// 登录
case 'login':
//判断请求是否合法
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//获取需要验证的数据
$email = $_POST['email'];
$password = sha1($_POST['password']);
// echo $email, $password;
// die;
$results = array_filter($users, function ($user) use ($email, $password) {
return $user['email'] === $email && $user['password'] === $password;
});
// print_r($results);
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('请求类型错误');
}
break;
//退出
case 'logout':
if (isset($_COOKIE['user'])) {
setcookie('user', null, time() - 3600);
exit('<script>alert("退出成功");location.assign("index.php")</script>');
}
break;
//注册
case 'reg';
//获取新用户信息
$name = $_POST['name'];
$email = $_POST['email'];
$password = sha1($_POST['password1']);
$time = time();
// echo $name, $email, $password, $time;
// die;
//2.写入新用户信息
$sql = "INSERT `user` SET `name`='{$name}',`email`='{$email}',`password`='{$password}',`time`={$time}";
$stmt = $pdo->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() === 1) exit('<script>alert("注册成功");location.assign("index.php")</script>');
else exit('<script>alert("注册失败");location.assign("index.php")</script>');
break;
default:
echo ('未定义操作');
}
3.处理用户注册登录脚本(SESSION)
<?php
session_start();
//处理脚本
$pdo = new PDO('mysql:host=localhost;dbname=php', 'root', 'root123456');
// var_dump($pdo);
// die;
$sql = 'SELECT * FROM `user` ';
$stmt = $pdo->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
// print_r($users);
//处理用户登录和注册
$action = $_GET['action'];
switch (strtolower($action)) {
// 登录
case 'login':
//判断请求是否合法
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//获取需要验证的数据
$email = $_POST['email'];
$password = sha1($_POST['password']);
// echo $email, $password;
// die;
$results = array_filter($users, function ($user) use ($email, $password) {
return $user['email'] === $email && $user['password'] === $password;
});
// print_r($results);
if (count($results) === 1) {
// setcookie('user', serialize(array_pop($results)));
$_SESSION['user'] = serialize(array_pop($results));
exit('<script>alert("验证通过");location.href="index.php"</script>');
} else {
exit('<script>alert("验证失败");location.href="login.php"</script>');
}
} else {
die('请求类型错误');
}
break;
//退出
case 'logout':
if (isset($_SESSION['user'])) {
// setcookie('user', null, time() - 3600);
session_destroy();
exit('<script>alert("退出成功");location.assign("index.php")</script>');
}
break;
//注册
case 'reg';
//获取新用户信息
$name = $_POST['name'];
$email = $_POST['email'];
$password = sha1($_POST['password1']);
$time = time();
// echo $name, $email, $password, $time;
// die;
//2.写入新用户信息
$sql = "INSERT `user` SET `name`='{$name}',`email`='{$email}',`password`='{$password}',`time`={$time}";
$stmt = $pdo->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() === 1) exit('<script>alert("注册成功");location.assign("index.php")</script>');
else exit('<script>alert("注册失败");location.assign("index.php")</script>');
break;
default:
echo ('未定义操作');
}
4. 前端代码分享
4.1 首页
<?php
session_start();
//判断是否已经登陆
if (isset($_SESSION['user'])) $user = unserialize($_SESSION['user']);
// print_r($user);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>首页</title>
<link rel="stylesheet" href="../css/index.css" />
</head>
<body>
<nav>
<div><a href="">logo</a></div>
<span>
<a href="index.php">官网首页</a>
<a href="index.php">HOME</a>
</span>
<span>
<a href="">活动中心</a>
<a href="">CONTENTS</a>
</span>
<span>
<a href="">新闻公告</a>
<a href="">NEWS</a>
</span>
<span>
<a href="">排行榜</a>
<a href="">LIST</a>
</span>
<span>
<a href="">兑换中心</a>
<a href="">EXCHANGE</a>
</span>
<span>
<a href="">博客之家</a>
<a href="">BLOG</a>
</span>
<span>
<a href="">发布任务</a>
<a href="">TASK</a>
</span>
<span>
<a href="">投诉建议</a>
<a href="">ADVICE</a>
</span>
<span>
<a href="">个人中心</a>
<a href="">CENTER</a>
</span>
<div>
<div>
<?php if (isset($user)) : ?>
<span>
<a href="" id="logout"><span style="color: red"><?php echo $user['name'] ?></span> 退出</>
</span>
<?php else : ?>
<span>
<a href="reg.php">注册</a>
<a href="login.php">登录</a>
</span>
<?php endif ?>
<a href="">HI,欢迎光临</a>
</div>
</div>
</nav>
</body>
<script>
// 为退出按钮创建事件监听器
document.querySelector('#logout').addEventListener('click', function(event) {
if (confirm('是否退出')) {
// 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
event.preventDefault();
// 跳转到退出事件处理器
window.location.assign('handle.php?action=logout');
}
});
</script>
</html>
4.2 注册页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>注册用户</title>
<link rel="stylesheet" href="../css/style.css" />
</head>
<body>
<div>
<div>
<div>账号注册</div>
<form action="handle.php?action=reg" method="POST" onsubmit="return compare()">
<section>
<label for="email">电子邮箱:</label>
<input type="email" name="email" id="email" required autofocus placeholder="请输入邮箱" />
</section>
<section>
<label for="name">用户昵称:</label>
<input type="text" name="name" id="name" required autofocus placeholder="请输入用户名称" />
</section>
<section>
<label for="password1">输入密码:</label>
<input type="password" name="password1" id="password1" required placeholder="请输入密码" />
</section>
<section>
<label for="password2">重复密码:</label>
<input type="password" name="password2" id="password2" required placeholder="请重复密码" />
</section>
<section>
<button>提交</button><span id="tips" style="color: red"></span>
</section>
</form>
<p>已有账号?请点击<a href="login.php">登录</a></p>
</div>
</div>
<script>
// 验证二次密码是否相等?
function compare() {
if (document.forms[0].password1.value.trim() !== document.forms[0].password2.value.trim()) {
document.querySelector('#tips').innerText = '二次密码不相等';
return false;
}
}
</script>
</body>
</html>
4.3 登陆页面
<?php
session_start();
//防止重复登陆
if (isset($_SESSION['user'])) exit('<script>alert("你已经登陆了");location.href="index.php";</script>');
// print_r($user);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>用户登录</title>
<link rel="stylesheet" href="../css/style.css" />
</head>
<body>
<div>
<div>
<div>账号登录</div>
<form action="handle.php?action=login" method="POST">
<section>
<label for="email">电子邮箱:</label>
<input type="email" name="email" id="email" required autofocus placeholder="请输入邮箱" />
</section>
<section>
<label for="password">输入密码:</label>
<input type="password" name="password" id="password" required placeholder="请输入密码" />
</section>
<button>提交</button>
</form>
<p>还没账号?请点击<a href="reg.php">注册</a></p>
</div>
</div>
</body>
</html>
5. css 样式分享
5.1 首页 index.css
body {
min-width: 1200px;
}
* {
padding: 0;
margin: 0;
}
a {
text-decoration: none;
color: #cdcde1;
}
nav {
display: flex;
height: 83px;
align-items: center;
padding: 0 20px;
justify-content: space-between;
background-color: #000;
}
nav > div:first-of-type {
width: 220px;
text-align: center;
}
nav > span {
width: 111px;
display: flex;
flex-direction: column;
align-items: center;
height: 83px;
justify-content: center;
}
nav > span > a {
padding: 5px;
}
nav > div:last-of-type > div {
display: flex;
height: 83px;
flex-direction: column;
align-items: center;
justify-content: space-evenly;
}
5.2 登陆页面注册页面样式 style.css
body {
min-width: 1200px;
margin: 0;
background: url(../bg.png) no-repeat;
height: 100%;
width: 100%;
overflow: hidden;
background-size: 100%;
display: flex;
justify-content: center;
/* align-items: center; */
}
body > div {
background: #fff;
width: 910px;
/* height: 968px; */
border-radius: 50px;
margin: 10px;
}
h3 {
text-align: center;
}
body > div > div {
height: 300px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-evenly;
}
body > div > div > div {
font-size: 2.5rem;
color: #e9cbac;
font-weight: bolder;
}
6. 进阶操作
- 验证码
//创建验证码
public function createCode(){
$strlen = strlen($this->string)-1;
for ($i=0; $i < $this->codeNum; $i++) {
$this->code .= $this->string[mt_rand(0,$strlen)]; //从字符集中随机取出四个字符拼接
}
$_SESSION['code'] = $this->code; //加入 session 中
//计算每个字符间距
$diff = $this->width/$this->codeNum;
for ($i=0; $i < $this->codeNum; $i++) {
//为每个字符生成颜色(使用深色)
$txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
//写入图像
imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
}
}
- 验证手机短信:正在学习如何使用别人的接口
7.总结
本节课学习了绘画控制的基本知识,简单地说,Cookie 是通过在客户端中记录信息而确定用户身份;Session 是通过在服务器端记录信息而确定用户身份。