1.什么是会话控制
- 会话指的用户的浏览器与服务器的信息交互
- 会话控制就是指控制保存浏览器与服务器的通话状态,
- 会话控制分两种方式:
cookie
和session
cookie
把信息保存在浏览器session
把信息保存在服务器
2.cookie和session
cookie
保存在浏览器,session
保存在服务器
设置和销毁cookie:
//setcookie(名称, 值, 过期时间);时间格式为时间戳
setcookie('username','admin', time()+60*10);
//获取cookie中的值
$_COOKIE['username'];
//销毁cookie,过去时间设置为过去时间
setcookie('username','admin', time()-10);
设置和销毁session:
//开启seeion会话
session_start();
//设置session
$_SESSION['username'] ='admin';
//获取session中的值
$_SESSION['username'];
//销毁session,可以使用session_destroy()、session_unset()
session_destroy();
3.实例演示
做一个演示页面,完成登录注册的功能
分别使用cookie和session两种方式保存登录信息
1.首页
<?php session_start();//开启session会话 ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>首页</title>
<style>
body>nav {
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
background-color: #400040;
/* width: 93vw; */
height: 80px;
padding: 20px 60px;
}
a {
text-decoration: none;
color: aliceblue;
font-size: 20px;
}
span>a {
text-decoration: none;
color: red;
font-size: 18px;
}
</style>
</head>
<body>
<!-- 导航栏 -->
<nav>
<a href="index.php">首页</a>
<?php ///*使用cookie方式*/ if(isset($_COOKIE['nickname'])):?>
<?php /*使用session方式*/ if(isset($_SESSION['nickname'])):?>
<span><a href='#'>欢迎您!
<?php //echo $_COOKIE['nickname'];?>
<?php echo $_SESSION['nickname'];?>
</a> <a href="handle.php?act=logout">退出</a></span>
<?php else:?>
<a href='login.php'>登录/注册</a>
<?php endif ?>
</nav>
</body>
</html>
效果图
2.登录页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>登录</title>
<style>
body {
display: grid;
justify-content: center;
align-items: center;
background-image: url('bg1.jpg');
background-size: 100vw 100vh;
}
.form {
margin-top: 220px;
width: 500px;
height: 300px;
background-color: #87a9c5;
border-radius: 5%;
display: flex;
flex-flow: column nowrap;
justify-content: space-around;
}
section {
padding: 0 30px;
display: flex;
align-items: center;
justify-content: center;
}
section > input {
height: 40px;
flex-grow: 1;
font-size: 1.5em;
}
section > label,
button {
width: 80px;
font-size: 1.5em;
/* flex-grow: 3; */
}
.form>:last-of-type{
margin-left: 30px ;
display: flex;
align-items: center;
justify-content: space-evenly;
}
</style>
</head>
<body>
<form action="handle.php?act=login" method="POST" class="form">
<section>
<label for="username">账号:</label
><input type="text" name="username" id="username" required/>
</section>
<section>
<label for="password">密码:</label
><input type="password" name="password" id="password" required/>
</section>
<section>
<button type="submit">登录</button>
<span>没有账号?点击<a href="register.php">注册</a>
</section>
</form>
</body>
</html>
效果图
3.注册页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>注册</title>
<style>
body {
background-color: #d4f3d4;
display: grid;
justify-content: center;
align-items: center;
background-image: url();
}
.form {
background-color: #acd6ac;
border: 1px solid #cccccc;
margin-top: 30px;
width: 500px;
height: 700px;
/* background-color: #cad6ca; */
border-radius: 2%;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: repeat(9, 1fr);
}
section {
height: 50px;
/* border-bottom: 1px solid;
border-top: 1px solid; */
padding: 0 30px;
display: flex;
flex-flow: row nowrap;
align-items: center;
justify-content: center;
}
section>input {
height: 30px;
flex-grow: 1;
font-size: 1.2em;
}
section>textarea {
height: 60px;
flex-grow: 1;
resize: none;
}
section>label,
button {
color: rgb(105, 76, 55);
width: 120px;
font-size: 1.3em;
text-align: center;
/* flex-grow: 3; */
}
.form> :last-of-type {
margin-left: 30px;
display: flex;
align-items: center;
justify-content: space-evenly;
}
h2 {
color: brown;
}
</style>
</head>
<body>
<form action="handle.php?act=register" method="POST" class="form">
<section>
<h2>用户注册</h2>
</section>
<section>
<label for="username">账号:</label><input type="text" name="username" id="username" autofocus />
</section>
<section>
<label for="nickname">昵称:</label><input type="text" name="nickname" id="nickname" required />
</section>
<section>
<label for="password1">密码:</label><input type="password" name="password1" id="password1" required />
</section>
<section>
<label for="password2">重复密码:</label><input type="password" name="password2" id="password2" required />
</section>
<section>
<label for="email">邮箱:</label><input type="email" name="email" id="email" required />
</section>
<section>
<label for="phone">电话:</label><input type="tel" name="phone" id="phone" />
</section>
<section>
<label for="replay">个人说明:</label><textarea type="text" name="replay" id="replay" /></textarea>
</section>
<section>
<button type="submit">提交</button>
<button type="reset">重置</button>
<span>已有账号?<a href="login.php">去登录</a></span>
</section>
</form>
</body>
</html>
效果图
4.请求处理页
<?php
session_start();//开启session会话
if(!isset($_GET['act'])){
exit('<script>alert("404!,未找到指向文件");location.href="index.php";</script>');
}
$dsn = "mysql:host=localhost;dbname=www.merchant.office;charset=utf8";
$sql_name = "merchant";
$sql_password = "merchant";
// $dsn,$sql_name,$sql_password
$action = $_GET['act'];
switch($action){
case 'logout':
setcookie('nickname',null,time()-10);//设置cookie过期,即销毁
session_destroy();//删除session
Header("location:index.php");
break;
case 'login' :
if(isset($_POST['username'])){
$username = $_POST['username'];
$password = md5($_POST['password']);
$sql = "select `nickname` from user where `username`='{$username}' and `password`='{$password}'";
try{
$pdo = new pdo($dsn,$sql_name,$sql_password);
$stmt = $pdo->prepare($sql);
$stmt->execute();
$res = $stmt->fetchAll();
if(!empty($res)){
setcookie('nickname',$res[0]['nickname'],time()+3600);//设置cookie
$_SESSION['nickname'] = $res[0]['nickname'];//设置session
header("location:index.php");
}else{
exit('<script>alert("登录失败,请检查账号密码!");location.href="login.php";</script>');
}
}catch(Exception $e){
$err = $e->getMessage();
exit("<script>alert('{$err}');location.href='login.php';</script>");
}
}else{
exit('<script>alert("请正确输入用户名和密码!");location.href="login.php";</script>');
}
break;
case 'register':
if(isset($_POST['username'])&&isset($_POST['nickname'])&&$_POST['password1']===$_POST['password2']){
extract($_POST);
$time = time();
$password=md5($_POST['password1']);
$sql = "insert user set `username`='{$username}',`password`='{$password}',
`nickname`='{$nickname}',`email`='{$email}',`replay`='{$replay}',`phone`='{$phone}',`create_time`='{$time}',`update_time`='{$time}'";
try{
$pdo = new pdo($dsn,$sql_name,$sql_password);
$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>');
}
}catch(Exception $e){
$err = $e->getMessage();
exit("<script>alert('{$err}');location.href='register.php';</script>");
}
}else{
exit('<script>alert("资料提交有误!");location.href="register.php";</script>');
}
break;
default:
exit('<script>alert("未知指令")</script>');
}
4. 总结
- cookie和session都可以保存信息,只不过保存的位置不一样。
- 记录用户信息的状态,在多个页面跳转时使用能提升用户感受
- cookie文件保存在用户本地,可能会被篡改,不用于保存重要信息