• 步步为营-75-Cookie简介


    说明:cookie常用于数据保存

    1 使用

      //创建cookie

      Response.cookies["yk"].value ="xyxtl";

      //设置过期时间     如果不设置cookie只存在内存中,无法实现长期保存

      Response.cookies["yk"].Expires = DateTime.Now.AddDays(3);

      //删除cookie  == 修改过期时间

      Response.cookies["yk"].Expires = DateTime.Now.AddDays(-3);

      //cookie跨域 正常的情况主域可以自动向子域传递数据,子域无法向主域传递数据

      Response.cookies["yk"].Domain = "主域网址";

      //cookie使用范围

       Response.cookies["yk"].Path = ...

    二:C#中的使用实例:记住密码

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="01Cookie记住密码.aspx.cs" Inherits="_03状态保持_Cookies和Session_._01Cookie记住密码" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
       
    </head>
    <body>
        <form id="form1" method="post" >
        用户名:<input type="text" name="UserName"/>
            <br />
        密码:<input type="password" name="Pwd"/>
            <br />
         <input type="checkbox" id="Remember" name="Remember"  />记住我1周    
         <input type="checkbox" id="Forget" name="Forget" />退出 
            <br />
         <input type="submit" value="登录" />
        </form>
    </body>
    </html>
    aspx
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace _03状态保持_Cookies和Session_
    {
        public partial class _01Cookie记住密码 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //写入cookie
                if (   Request["Remember"]=="on")
                {
                    string userName = Request["UserName"];
                    string pwd = Request["Pwd"];
                    Response.Cookies[userName].Value = pwd;
                    Response.Cookies[userName].Expires = DateTime.Now.AddDays(7);
    
                }
                //删除cookie
                if ( Request["Forget"] == "on")
                {
                    string userName = Request["UserName"];
                    string pwd = Request["Pwd"];
                    Response.Cookies[userName].Value = pwd;
                    Response.Cookies[userName].Expires = DateTime.Now.AddDays(-3);
    
                }
                //判断cookie
                if ( (Request.Cookies["yk"]) != null)
                {
                    if (Request.Cookies["yk"].Value == "123")
                    {
                        Response.Write("登录成功!");
                         Response.Write(Request["category"]);
                    }
                    else {
                        Response.Write("登录失败!");
                        Response.Write(Request["category"]);
                    }
                }
                else 
                {
                    if (!String.IsNullOrEmpty(Request["UserName"]) && !String.IsNullOrEmpty(Request["Pwd"]) && Request["UserName"] == "yk" && Request["Pwd"]=="123")
                    {
                        Response.Write("登录成功!");
                        Response.Write(Request["category"]);
                    }
                    else {
                        Response.Write("登录失败!");
                          Response.Write(Request["category"]);
                    }
                }
            }
        }
    }
    aspx.cs

    在不使用cookie记录前登录是失败的


    三:python 中使用cookie直接访问index页面

    1:注释掉setting.py的中间件配置

     

    2:配置路由

    """cookie_Session URL Configuration
    
    The `urlpatterns` list routes URLs to views. For more information please see:
        https://docs.djangoproject.com/en/2.1/topics/http/urls/
    Examples:
    Function views
        1. Add an import:  from my_app import views
        2. Add a URL to urlpatterns:  path('', views.home, name='home')
    Class-based views
        1. Add an import:  from other_app.views import Home
        2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
    Including another URLconf
        1. Import the include() function: from django.urls import include, path
        2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
    """
    from django.contrib import admin
    from django.urls import path
    from app011 import views
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('login/', views.login),
        path('index/', views.index),
    
    ]
    urls.py
    from django.shortcuts import render,redirect
    
    # Create your views here.
    def login(request):
        # print("SSSSSSSSS",request.session)
        if request.method=="POST":
            name=request.POST.get("user")
            pwd = request.POST.get("pwd")
            if  name=="aaron" and pwd=="1":
                ret = redirect("/index/")
                ret.set_cookie("user",name)
                ret.set_cookie("password",pwd)
                return ret
        return render(request,"login.html")
    
    def index(request):
    
        if  request.COOKIES.get("user",None) == "aaron":
            name = "aaron"
            return render(request, "index.html",locals())
        else:
            return redirect("/login/")
    Views.py
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <h1>{{ name }}</h1>
    </body>
    </html>
    index.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>登录</title>
        <style>
            *{
                margin: 0;
                padding:0;
            }
        </style>
    </head>
    <body>
    <form action="/login/" method="post">
        <p>姓名<input type="text" name="user"></p>
        <p>密码<input type="password" name="pwd"></p>
        <p><input type="submit"></p>
    
    </form>
    </body>
    </html>
    login.html

  • 相关阅读:
    <<C++ Primer>> 第三章 字符串, 向量和数组 术语表
    <<C++ Primer>> 第二章 变量和基本类型 术语表
    <<C++ Primer>> 第一章 开始 术语表
    PAT A1077 Kuchiguse (20)
    PAT A1035 Password (20)
    PAT A1005 Spell It Right (20)
    <<C++ Primer>> 术语表 (总) (待补充)
    PAT A1001 A+B Format (20 分)
    PAT B1048 数字加密 (20)
    Protocol
  • 原文地址:https://www.cnblogs.com/YK2012/p/7029246.html
Copyright © 2020-2023  润新知