码迷,mamicode.com
首页 > 其他好文 > 详细

cookie和session

时间:2018-07-05 00:35:28      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:运用   NPU   htm   跳转   als   就是   har   object   hid   

 

cookie和session的介绍

 cookie的工作原理是:由服务器产生内容,浏览器收到请求后保存在本地;当浏览器再次访问时,浏览器会自动带上cookie,这样服务器就能通过cookie的内容来判断这个是“谁”了。

其实就是你的浏览器现在都运用这个技术  你的浏览器发送一个请求去服务器 这个时候浏览器封装了一个cookie 这个cookie自带一个空的字典去请求服务器 然后登陆成功后你的服务器个这个字典填充内容  ,然后浏览器下次再请求这个服务器的其他界面就不用再次登陆了 。

最早的时候没有cookie技术是访问一个网站的每一个界面都需要登陆的  这样是很麻烦的

 

 

cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生。

 

cookie的简单使用:

1、获取Cookie

request.COOKIES.get("islogin",None)  #如果有就获取,没有就默认为none

 

2、设置Cookie

  obj = redirect("/index/")  #生成一个对象 为了让这个对象进行设置cookies值
  obj.set_cookie("islogin",True)  #设置cookie值,注意这里的参数,一个是键,一个是值
  obj.set_cookie("haiyan","344",20)  #20代表过期时间
  obj.set_cookie("username", username)

 

3、删除Cookie

obj.delete_cookie("cookie_key",path="/",domain=name)

 

 

from ssb_one .models import User
# Create your views here.

import datetime
def login(request):
    if request.method == POST:
        user = request.POST.get(user)
        pwd = request.POST.get(pwd)

        if User.objects.filter(user = user,pwd = pwd):
            obj = redirect(/index/)  #登陆成功定向到index页面这个功能赋予了一个对象 通过这个对象进行cookies的设置
            obj.set_cookie(username,user)
            obj.set_cookie(is_login,True)# 因为你的浏览器访问的时候就自带了cookie并且这个cookie自带了一个空字典 你返回一个带参数的字典给你的浏览器  然后浏览器下次再访问就会带着这个带参数的字典访问 服务器就知道是这个浏览器又访问你了
            obj.set_cookie(login_time,datetime.datetime.now())
            return obj   #这一步是把你的 对象返回定向页面 因为你的obj这个是皇后就代表你的定向页面就是执行重定向

    return render(request,login.html)



def index(request):
    print(request.COOKIES)   #你cookies访问后  服务器返回的内容在COOKIES内
    is_login = request.COOKIES.get(is_login)  #如果你返回的不是一个接收过来的cookies内的字典的内容就代表没有登陆成功
    if not is_login:  #没有登陆成功
        return redirect(/login/)

    username = request.COOKIES.get(username)
    login_time = request.COOKIES.get(login_time)
    return render(request,index.html,locals())

 

 

 

 

一个简单的登陆跳转界面然后不需要登陆的界面

urls

技术分享图片
from django.contrib import admin
from django.urls import path
from ssb_one import views

urlpatterns = [
    path(admin/, admin.site.urls),
    path(login/,views.login),
    path(index/,views.index),
]
urls

 

models

技术分享图片
from django.db import models

# Create your models here.
class User(models.Model):
    user = models.CharField(max_length = 30)
    pwd = models.IntegerField()
models

 

views:

技术分享图片
from ssb_one .models import User
# Create your views here.

import datetime
def login(request):
    if request.method == POST:
        user = request.POST.get(user)
        pwd = request.POST.get(pwd)

        if User.objects.filter(user = user,pwd = pwd):
            obj = redirect(/index/)  #登陆成功定向到index页面这个功能赋予了一个对象 通过这个对象进行cookies的设置
            obj.set_cookie(username,user)
            obj.set_cookie(is_login,True)# 因为你的浏览器访问的时候就自带了cookie并且这个cookie自带了一个空字典 你返回一个带参数的字典给你的浏览器  然后浏览器下次再访问就会带着这个带参数的字典访问 服务器就知道是这个浏览器又访问你了
            obj.set_cookie(login_time,datetime.datetime.now())
            return obj   #这一步是把你的 对象返回定向页面 因为你的obj这个是皇后就代表你的定向页面就是执行重定向

    return render(request,login.html)



def index(request):
    print(request.COOKIES)   #你cookies访问后  服务器返回的内容在COOKIES内
    is_login = request.COOKIES.get(is_login)  #如果你返回的不是一个接收过来的cookies内的字典的内容就代表没有登陆成功
    if not is_login:  #没有登陆成功
        return redirect(/login/)

    username = request.COOKIES.get(username)
    login_time = request.COOKIES.get(login_time)
    return render(request,index.html,locals())
views

 

index:

技术分享图片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h3>上次登陆时间:{{ login_time }}</h3><br/>


<p>Hi! {{ username }}</p>

</body>
</html>
index

 

login:

技术分享图片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form action="" method = post>
    <input type="text" name="user">
    <input type="password" name = pwd>
    <input type="submit" value="提交">
</form>

</body>
</html>
View Code

 

 

cookie存储到客户端

优点:数据存储在客户端。减轻服务端的压力,提高网站的性能

缺点:安全性不高,在客户端很容易被查看或破解用户会话信息

 

cookie和session

标签:运用   NPU   htm   跳转   als   就是   har   object   hid   

原文地址:https://www.cnblogs.com/askzyl/p/9266067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!