新密建设局网站,淘宝网页设计代码,wordpress建社区,广告制作的软件【一】Cookie与Session
Cookie和Session是用来在Web应用程序中跟踪用户会话数据的两种常用技术。
【1】Cookie和Session的发展史
【1】Cookie的发展史#xff1a;
1994年#xff0c;网景通信公司推出了第一个浏览器Cookie技术。Cookie是存储在用户计算机上的小型文本文件…【一】Cookie与Session
Cookie和Session是用来在Web应用程序中跟踪用户会话数据的两种常用技术。
【1】Cookie和Session的发展史
【1】Cookie的发展史
1994年网景通信公司推出了第一个浏览器Cookie技术。Cookie是存储在用户计算机上的小型文本文件用于跟踪用户在网站上的活动。初始版本的Cookie只能存储很少的数据并且没有强制加密机制容易被恶意用户篡改或窃取。因此随着互联网的快速发展Cookie引起了一系列安全和隐私问题。
【2】Session的发展史
由于Cookie存在的局限性Web开发人员开始寻找更安全、可靠的替代方案。1997年Sun Microsystems提出了基于服务器的会话管理方案即Session。Session是在服务器端存储用户会话数据的一种技术。每当用户访问网站时服务器会为其创建一个唯一的Session标识符(Session ID)并将会话数据存储在服务器上。Session ID一般通过Cookie或URL参数传递给客户端用于识别用户的会话状态。
【2】Cookie和Session的关系
在实际应用中Cookie和Session通常结合使用。当用户首次访问网站时服务器会为其分配一个唯一的Session ID并将其存储在Cookie中发送给客户端保存。随后客户端在每次请求中都会携带该Cookie服务器通过解析Cookie中的Session ID读取对应的会话数据实现用户状态的跟踪和管理。
【1】总结
Cookie和Session是Web应用程序中常用的用户会话跟踪技术。Cookie通过在客户端存储小型文本文件并将会话标识符传递给服务器实现会话状态的保持。而Session则是在服务器端存储会话数据通过Session ID实现对用户会话的追踪。
它们的发展历程与互联网的发展紧密相关为开发人员提供了更多的选择以保障安全性和用户体验的提升。
【3】Cookie与Session详解
【1】Cookie
服务器保存在客户端浏览器上的信息都可以称之为cookie它的表现形式一般都是k:v键值对(可以有多个)
【2】Session
保存在服务器上的信息都可以称之为session它的表现形式一般都是k:v键值对(可以有多个)
【3】token
session虽然数据是保存在服务端的但是挡不住数据量大解决办法服务端不再保存数据 登陆成功之后将一段信息加密处理(用自己独特的加密方式进行加密)
将加密之后的结果拼接在信息后面整体返回给浏览器保存浏览器下次访问的时候带着该信息服务端自动切取前面的一段信息再次使用自己的加密算法进行加密然后用这段密文与携带过来的密文进行比对
【4】总结
cookie就是保存在客户端浏览器上的信息session就是保存在服务端上的信息
session是基于cookie工作的(其实大部分的保存用户状态的操作都需要使用cookie)
【2】Django操作Cookie 虽然cookie是服务端告诉客户端浏览器需要保存内容但是客户端浏览器可以选择拒绝保存如果禁止自动保存cookie 那么只要是需要登录的网站都没办法正常登录了 【1】三板斧-视图函数返回
- return HttpResponse()
- return render()
- return redirect()
obj HttpResponse(ok)
obj.set_cookie(k,v)
【2】设置cookie
def login(request, *args, **kwargs):if request.method POST:username request.POST.get(username)password request.POST.get(password)if username dream and password 521:obj HttpResponse(ok)obj.set_cookie(sign, user)return objelse:return redirect(/login/)return render(request, login.html)
取值cookie验证
def home(request, *args, **kwargs):sign request.COOKIES.get(sign)if sign and sign user:return HttpResponse(这是home页面)else:return redirect(/login/)
【3】取值
request.COOKIES.get(k)
【4】设置超时时间
obj.set_cookie(sign, user, expires3)
obj.set_cookie(sign, user, max_age3)
#设置超时时间3s到期 max_age 设置超时时间以秒为单位 expiress 设置超时时间 针对IE浏览器使用以秒为单位
【5】注销cookie
def logout(request, *args, **kwargs):obj redirect(/home/)# 设置超时时间 3s 到期obj.delete_cookie(sign)return obj
【6】完整版 cookie登录注册
def login(request, *args, **kwargs):# next_url request.get_full_path()# print(next_url) # /login/?next_url/home/if request.method POST:username request.POST.get(username)password request.POST.get(password)if username dream and password 521:next_url request.GET.get(next_url)# print(next_url) # /home/obj redirect(next_url)obj.set_cookie(sign, user)return objelse:return redirect(/login/)return render(request, login.html)def login_auth(func):def inner(request, *args, **kwargs):# print(request.path_info) # /home/# print(request.get_full_path()) # /home/?username111next_url request.get_full_path() # /home/# print(next_url)# /home/sign request.COOKIES.get(sign)if sign and sign user:res func(request, *args, **kwargs)return reselse:return redirect(f/login/?next_url{next_url})return innerlogin_auth
def home(request, *args, **kwargs):return HttpResponse(这是home页面)# def home(request, *args, **kwargs):
# sign request.COOKIES.get(sign)
# if sign and sign user:
# return HttpResponse(这是home页面)
# else:
# return redirect(/login/)login_auth
def index(request, *args, **kwargs):return HttpResponse(这是index页面)
【3】Django操作Session session数据是保存在服务端的给客户端返回的是一个随机字符串 sessionid随机字符串 【1】设置Session
request.session[sign] user
【2】取值session
sign request.session.get(sign)
【3】session设置过期时间 request.session[sign] user# 如果是数字的话就是指定 s shu# request.session.set_expiry(3)# 0 就是关闭浏览器后自动清除浏览器的sessionidrequest.session.set_expiry(0)
参数 整数 多少秒过期日期对象 到指定日期失效0 一旦退出当前浏览器窗口就失效不写 失效时间取决于Django内部全局session失效的时间
【4】删除session # 删除session方式一# request.session.delete()# 把浏览器和数据库里面的session全部清除掉request.session.flush()
(1)request.session.delete(): 只删除服务端的 该方法用于删除当前用户的Session数据但会保留Session的Key。这意味着Session对象本身仍然存在但其中的数据将被清空。下次访问时如果Session没有被重新填充则会得到一个空的Session对象。
(2)request.session.flush(): 服务端和客户端都删除 该方法用于完全删除当前用户的Session包括Session对象和所有相关数据。下次访问时将创建一个新的空Session对象。
【5】注意 session基于数据库表才能使用的 必须先迁移数据库生成 django_session 表 session只对当次登录有效 主动清除浏览器中本地存在的session 验签发现没有sessionid就会自动生成新的session django_sessoin表中的数据条数取决于浏览器 同一个计算机(IP地址)上同一个浏览器只会有一条数据生效 同一个计算机(IP地址)上多个浏览器会有多个数据生效 当session过期的时候可能会出现多条数据对应一个浏览器 但是这些数据不会持久化存储会被定时清理掉可以手动清除也可以代码清除 目的是为了节省服务器数据库资源
def login(request, *args, **kwargs):# next_url request.get_full_path()# print(next_url) # /login/?next_url/home/if request.method POST:username request.POST.get(username)password request.POST.get(password)if username dream and password 521:# next_url request.GET.get(next_url)# print(next_url) # /home/request.session[sign] userobj redirect(/home/)# 设置过期时间# obj.set_cookie(sign, user, expires3)# obj.set_cookie(sign, user, max_age3)return objelse:return redirect(/login/)return render(request, login.html)def login_auth(func):def inner(request, *args, **kwargs):# print(request.path_info) # /home/# print(request.get_full_path()) # /home/?username111next_url request.get_full_path() # /home/# print(next_url)# /home/sign request.session.get(sign)# print(sign) # userif sign and sign user:res func(request, *args, **kwargs)return reselse:return redirect(f/login/?next_url{next_url})return innerlogin_auth
def home(request, *args, **kwargs):return HttpResponse(这是home页面)
【二】 CBV加装饰器的三种方法
from django.utils.decorators import method_decorator
方式一加载视图函数上面 method_decorator(login_auth)def get(self, request, *args, **kwargs):return HttpResponse(这是home页面)def post(self):
方式二放在类视图上面 (放的装饰器函数,name指定你的视图函数里面的方法)
# method_decorator(login_auth, nameget)
# method_decorator(login_auth, namepost)
class UserView(View):
方式三 dispactch 方法加装饰器 本视图函数内所有的视图都需要走装饰器 method_decorator(login_auth)def dispatch(self, request, *args, **kwargs):# Try to dispatch to the right method; if a method doesnt exist,# defer to the error handler. Also defer to the error handler if the# request method isnt on the approved list.if request.method.lower() in self.http_method_names:handler getattr(self, request.method.lower(), self.http_method_not_allowed)else:handler self.http_method_not_allowedreturn handler(request, *args, **kwargs)