说明: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. oracle启用归档日志

    一.开启归档 1.查看归档信息 SQL> archive log list Database log mode No Archive Mode Automatic archival Disabl ...

  2. Delaunay triangulation

    1,先花个圆: detail模式执行. #define XY 0x00 #define XZ 0x01 #define YZ 0x02 #define pi 3.1415926 #define clo ...

  3. Delphi 的 FireDAC 连接管理与配置过程

    Delphi 的 FireDAC 连接管理与配置过程: 使用 FireDAC 技术连接 数据库,主要是使用  TFDConnection ,其中有一参数是选择  ConnectionDefFile. ...

  4. Centos下配置php环境

    Centos下配置php环境   目录[-] 环境: GD2 2 安装PHP 5.2.14(FastCGI模式) 1)编译安装PHP 5.2.14所需的支持库: 2)编译安装MySQL 5.5.3-m ...

  5. bigfile tablespace

    背景       这次终于有个linux实际迁移oracle的机会了,之前都是学习实验.想起最早时,都是windows搞oracle,又让我想起多年期一个项目,数据量太大及计算逻辑太复杂,我用存储过程 ...

  6. mysql select in 怎么优化

    2017年7月10日 22:36:54 星期一 原理: 把select where in 变换成 "where = " 或者 "where between and &qu ...

  7. python---控制台输出带颜色的文字方法

    控制台的展示效果有限,并不能像前端一样炫酷,只能做一些简单的设置,不过站在可读性的角度来看,已经好很多了. 书写格式: ##格式: 设置颜色开始:\033[显示方式;前景色;背景色m ##说明: 前景 ...

  8. CLOB数据类型截取SUBSTR_按开始位置偏移量

    --DBMS_LOB.substr不加参数表示全部截取,负向截取待定 CREATE OR REPLACE FUNCTION CLOB_SUBSTR( V_CLOB CLOB, N_OFFSET NUM ...

  9. servlet web.xml配置选项详解

    一般的web工程中都会用到web.xml,web.xml主要包括一些配置标签,例如Filter.Listener.Servlet等,可以用来预设容器的配置,可以方便的开发web工程.但是web.xml ...

  10. table中border-collapse的问题

    在table中,如果设置了border-collapse: collapse;,边框会合并,这时你修改top或bottom的颜色,会有问题 解决办法是:border-collapse: separat ...