说明: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. 微信支付-H5网页支付开通流程

    简介  H5 支付是指商户在微信客户端外的移动端网页展示商品或服务,用户在前述页面确认使用微信支付时,商户发起本服务呼起微信客户端进行支付.主要用于触屏版的手机浏览器请求微信支付的场景.可以方便的从外 ...

  2. 图像超分辨-IDN

    本文译自2018CVPR Fast and Accurate Single Image Super-Resolution via Information Distillation Network 代码 ...

  3. zynq linux驱动之PL-PS中断【转】

    转自:https://blog.csdn.net/h244259402/article/details/83993524 PC:Windows 10 虚拟机:ubuntu 16.04 vivado:2 ...

  4. http://nancyfx.org + ASPNETCORE

    商务产品servicestack:  https://servicestack.net/ http://nancyfx.org  +  ASPNETCORE http://nancyfx.org    ...

  5. 使用jsencrypt(rsa加密方式)给js加密防被刷

    加密步骤 1.需要加密的参数 * * ).toISOString().replace(/T/g, }Z/, ''); //使用本地时间,然后转换格式 2.js中引用jsencrypt.js文件,然后实 ...

  6. Yarn vs npm: 你需要知道的一切

    Yarn 是 Facebook, Google, Exponent 和 Tilde 开发的一款新的 JavaScript 包管理工具.就像我们可以从官方文档了解那样,它的目的是解决这些团队使用 npm ...

  7. C/C++中可变参数函数的实现

    在C语言的stdarg.h头文件中提供了三个函数va_start, va_end,va_arg和一个类型va_list.利用它们,我们可以很容易实现一个可变参数的函数.首先简单介绍一下这三个函数. 假 ...

  8. bootstrap简单使用布局、栅格系统、modal标签页等常用组件入门

    <!DOCTYPE html> <html> <head> <title>bootstrap</title> <!-- 引入boots ...

  9. LightOJ1004

    #include<bits/stdc++.h> using namespace std; int Map[106][106]; int Vis[106][106]; int Num[106 ...

  10. 【原创】大数据基础之Hive(5)hive on spark

    hive 2.3.4 on spark 2.4.0 Hive on Spark provides Hive with the ability to utilize Apache Spark as it ...