步步为营-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(); }
//删除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记录前登录是失败的

步步为营-75-Cookie简介

步步为营-75-Cookie简介
三:python 中使用cookie直接访问index页面

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

步步为营-75-Cookie简介

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简介

上一篇:SQL高级查询——50句查询(含答案) ---参考别人的,感觉很好就记录下来留着自己看。


下一篇:CenOS_文件目录类操作命令