说明: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(); }
//删除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(-); }
//判断cookie
if ( (Request.Cookies["yk"]) != null)
{
if (Request.Cookies["yk"].Value == "")
{
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"]=="")
{
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=="":
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

步步为营-75-Cookie简介的更多相关文章

  1. Session & Cookie 简介

    (一)简介 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通 ...

  2. cookie简介

    上例子 1.首先要用php创建cookie发送给客户端,利用setcookie()方法即可 <?php /* * * @Authors peng--jun * @Email 1098325951 ...

  3. session cookie简介

    会话机制:Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端记录信息确定用户身 ...

  4. Session&Cookie 简介及使用

    Cookie cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 或其它语言来创建和取回 cookie ...

  5. 步步为营-36-ADO.Net简介

    与数据库进行连接交互 方法一 #region 01连接对象 //01 连接字符串 string connstr = "server=.;uid=sa;pwd=sa;database=Demo ...

  6. cookie技术简介

    Cookie简介 众所周知,HTTP协议是一个无状态的协议.一个Web应用由很多个Web页面组成,每个页面都有唯一的URL来定义.用户在浏览器的地址栏输入页面的URL,浏览器就会向Web Server ...

  7. 《AngularJS入门与进阶》图书简介

    一.图书封面 二.图书CIP信息 图书在版编目(CIP)数据 AngularJS入门与进阶 / 江荣波著. – 北京 : 清华大学出版社, 2017 ISBN 978-7-302-46074-9 Ⅰ. ...

  8. C#中Cookie的概述及应用

    1.Cookie简介 Cookie 提供了一种在 Web 应用程序中存储用户特定信息的方法.例如,当用户访问您的站点时,您可以使用 Cookie 存储用户首选项或其他信息.当该用户再次访问您的网站时, ...

  9. Asp.net操作cookie大全

    实例代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3 ...

  10. [转载]JavaEE学习篇之——Session&&Cookie

    原文链接: http://blog.csdn.net/jiangwei0910410003/article/details/23337043 今天继续来看看JavaWeb的相关知识,这篇文章主要来讲一 ...

随机推荐

  1. 51nod 1294 修改数组

    若a[i]-i(i从1开始)的值小于0,那么a[i]必须改变 若a[i]-i的值大于等于0,将a[i]-i存入新的数组中,求出新数组的最长非严格上升子序列,所得即最多的,不用改变的数. #includ ...

  2. Nginx系列5:从网络原理来看SSL安全协议

    1.TLS/SSL发展 2.TLS安全密码套件解读

  3. 【转】python之配置日志的几种方式

    [转]python之配置日志的几种方式 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用 ...

  4. 记录一下putty的pscp的用法【转】

     转自 记录一下putty的pscp的用法 - 刘荣星的博客 https://www.liurongxing.com/how-use-the-putty-and-pscp.html 以前一直用Secu ...

  5. git与eclipse集成之更新特性分支代码到个人特性分支

    1.1. 更新特性分支代码到个人特性分支 在基于特性分支开发的过程中,存在多人向特性分支提交代码的情况,开发者需要关注特性分支代码与个人分支代码保持同步,否则可能导致提交代码冲突. 具体代码同步步骤: ...

  6. localtime函数和strftime函数

    localtime函数 功能: 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,而gmtime函数转换后的时间没有经过时区变换,是UTC时间 . 用法: #include & ...

  7. $Django 客户端->wsgi->中间组件->urls->views(model,template) 总结+补充(事物,choices,inclusion_tag)!

    1 HTTP协议:(重点)  -请求   -请求首行    -GET /index HTTP/1.1 \r\n   -请求头部    -key:value------>\r\n分割    _ke ...

  8. sqlserver2008r2还原完整备份和差异备份及自动删除过期备份

    本文主要内容: 还原完整和差异备份 删除超过1个月的备份 注:保证SQL Server代理服务启动,并把服务设置为自动启动 完整备份和差异备份还原原理: 差异备份是完整备份的补充,只备份上次完整备份后 ...

  9. django.db.utils.OperationalError: (1049, "Unknown database 'djangodb'")

    DATABASES = { 'default': { 'ENGINE':'django.db.backends.mysql', 'NAME': 'mysql', 'USER':'root', 'PAS ...

  10. 小白6步搞定vue脚手架创建项目

    1.安装node及npm node -v (测试node是否安装成功)npm -v(测试npm是否安装成功) 2.安装cnpm npm install -g cnpm --registry=http: ...