当前位置:网站首页>Difference between cookie and session
Difference between cookie and session
2022-07-30 06:05:00 【burial】
cookie
1.什么是cookie
Cookie意为“甜饼”,是由W3C组织提出,最早由Netscape社区发展的一种机制.目前Cookie已经成为标准,所有的主流浏览器如IE、Netscape、Firefox、Opera等都支持Cookie.
2.为什么要用cookie
由于http协议是一种无状态的协议(客户端和服务端互相不认识)
Cookies是一些存储在用户电脑上的小文件.它是被设计用来保存一些站点的用户数据,这样能够让服务器为这样的用户定制内容.页面代码能够获取到Cookie值然后发送给服务器,比如Cookie中存储了所在地理位置,以后每次进入地图就可以默认定位到改地点.
3.cookie的原理
cookie的执行原理:就是当客户端访问服务器的时候(服务器运用了cookie),服务器会生成一份cookie传输给客户端,客户端会自动把cookie保存起来,以后客户端每次访问服务器,都会自动的携带着这份cookie.
简单来说,就是当客户端访问服务器时,服务器会生成一个票据给客户端,当客户端收到票据的之后就保存起来,以后再访问服务器就会自动带着票据.
- 简易cookie原理示意图****如下图所示:

4.安装使用cookie
1.安装
npm i cookie-parser
注:Scaffolding can be used directly
express --view=模板引擎名称 项目名称
2.使用
在脚手架app.jsalready included in the cookie:
var cookieParser = require(‘cookie-parser’);
app.use(cookieParser());//使用cookie-parser
生成cookie:
//设置cookie
//在响应的时候,生成一份cookie(票据)
// res.cookie(‘xxcode’,“abc”);
在浏览器中显示:

取出cookie:
console.log(req.cookies);
在后端显示:

5.加密cookie
在脚手架app.jsalready included in the cookie:
var cookieParser = require(‘cookie-parser’);
app.use(cookieParser(“xiaoxuesheng”));//签名 (加密) To specify the key ,Any name is a star,列如:“xiaoxuesheng”
在users.js中
//使用cookie
//加密存储
res.cookie(“mycode”,“abc”,{
signed:true //加密
})//取出cookie
let mycode = req.signedCookies.mycode
5.cookie的特点:
保存到客户端,容易被篡改(删除、禁用)
保存的数据量有限的
简单易懂好用
6.cookie的缺点:
- 保存在客户端,容易被篡改.
- 大小受限,本身最大4kb.
session
1.什么是session
Session是另一种记录客户状态的机制,不同的是Cookie保存在客户端浏览器中,而Session保存在服务器上.客户端浏览器访问服务器的时候,服务器把客户端信息以某种形式记录在服务器上.这就是Session.客户端浏览器再次访问时只需要从该Session中查找该客户的状态就可以了session是一种特殊的cookie.cookie是保存在客户端的,而session是保存在服务端.
2.为什么要用session
由于cookie 是存在用户端,而且它本身存储的尺寸大小也有限,最关键是用户可以是可见的,并可以随意的修改,很不安全.那如何又要安全,又可以方便的全局读取信息呢?于是,这个时候,一种新的存储会话机制:session 诞生了
3.session原理
当客户端第一次请求服务器的时候,服务器生成一份session保存在服务端,将该数据(session)的id以cookie的形式传递给客户端;以后的每次请求,浏览器都会自动的携带cookie来访问服务器(session数据id).
- 简易cookie原理示意图****如下图所示:

4.安装使用session
1.安装
npm i express-session
2.使用
//引入session中间件
let expressSession=require(“express-session”)//使用session中间件
app.use(expressSession({
resave:true,//Whether to refresh the memory every time
saveUninitialized:true,
secret:“xiaoxuesheng” //秘钥
}))
3.代码示例
(1)使用session存储数据:
// 存储
router.get("/test", (req, res) => {
req.session.yourcode = 'abc';
res.send('ok')
})
在浏览器中显示:

will showid和后面的字符串,后端打印的“abc”没出现
(2)取出session数据:
router.get("/test2", (req, res) => {
console.log(req.session);
res.send("ookkkk")
})
在后端显示:

4.The results that can be achieved on different ends are different
Such as in Firefox browser:

5.cookie和session的共同点
cookie和session都是用来跟踪浏览器用户身份的会话方式.
6.cookie和session区别
cookie是保存在客户端的
cookie有大小限制
session是保存在服务器端
session更加安全
session会比较占用服务器性能,当访问增多时应用cookie
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在.深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
- MySQL安装配置教程(超级详细)
- MySQL stored procedure
- "Hou Lang" programmer version, a speech dedicated to a new generation of programmers, He Bing's "Hou Lang" speech imitation show
- 【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
- MySQL - Function and Constraint Commands
- 4、nerf(pytorch)
- Golang——从入门到放弃
- Path dependence: the poor hard science to counter attack breakthrough
- Detailed MySQL-Explain
猜你喜欢

期末作业C#实现学生宿舍管理系统

PyCharm usage tutorial (more detailed, picture + text)

一文带你吃透js处理树状结构数据的增删改查

JVM 类加载机制 超详细学习笔记(三)

2022 SQL big factory high-frequency practical interview questions (detailed analysis)

留念 · 大学时代最后的系统设计图

Participate in open source, let programmers regain their blood and passion

从想当亿万富翁到职场、创业、爱情、抑郁、学医学武,我的程序人生
![[Redis Master Cultivation Road] Jedis - the basic use of Jedis](/img/e3/0c6efd03432a01f857796f0bf648ef.png)
[Redis Master Cultivation Road] Jedis - the basic use of Jedis

SOA(面向服务架构)是什么?
随机推荐
期末作业C#实现学生宿舍管理系统
Error: npm ERR code EPERM
【图像处理】基于中轴变换实现图像骨架提取附matlab代码
MySQL kills 10 questions, how many questions can you stick to?
[Koltin Flow (2)] The end operator of the Flow operator
108. 将有序数组转换为二叉搜索树
使用DataEase开源工具制作一个高质量的数据大屏
Mysql8.+学习笔记
MySql字符串拆分实现split功能(字段分割转列、转行)
429. N 叉树的层序遍历(两种解法)
MySQL Basics (DDL, DML, DQL)
破纪录者(Google Kickstart2020 Round D Problem A)
MySQL 灵魂 16 问,你能撑到第几问?
ClickHouse 数据插入、更新与删除操作 SQL
Learn FPGA from the underlying structure (6) ---- Distributed RAM (DRAM, Distributed RAM)
navicat无法连接mysql超详细处理方法
cmd(命令行)操作或连接mysql数据库,以及创建数据库与表
成绩排序(华中科技大学考研机试题)(DAY 87)
It's time to have to learn English, give yourself multiple paths
程序员赚钱实操,手把手教你做付费课程,自媒体,付费文章及付费技术课赚钱