zoukankan      html  css  js  c++  java
  • 创建cookie

    《ASP.NET 3.5揭秘》读书笔记

    cookie 分为:会话cookie和持久化cookie两种。

    会话cookie只存在于内存中;

    持久化cookie可以存在几个月甚至几年,持久化cookie创建后,会被浏览器长久地保存在用户的电脑上。(如IE存在这个文件夹里:\Documents and Settings\[user]\Cookies)

    创建会话cookie。

    通过Response.Cookies集合添加cookie来创建新的cookie。

    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Cookies["message"].Value = txtCookieValue.Text; 
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Set Cookie</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        <asp:Label
            id="lblCookieValue"
            Text="Cookie Value:"
            AssociatedControlID="txtCookieValue"
            Runat="server"/>
        <asp:TextBox
            id="txtCookieValue"
            Runat="server" />
        <asp:Button
            id="btnAdd"
            Text="Add Value"
            OnClick="btnAdd_Click"
            Runat="server"/>
        </div>
        </form>
    </body>
    </html>

    创建持久cookie。

    如果希望创建持久化cookies,则需要为cookie指定一个过期时间。如下面程序中Respo.Cookies["counter"].Expires = DataTime.Now.AddYears(2);

    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        void Page_Load()
        {
             // Get current value of cookie
            int counter = 0;
            if (Request.Cookies["counter"] != null)
            {
                counter = Int32.Parse(Request.Cookies["counter"].Value);
            }
            
            // Increment counter
            counter++;
            
            // Add Peristent cookie to browser
            Response.Cookies["counter"].Value = counter.ToString();
            Response.Cookies["counter"].Expires = DateTime.Now.AddYears(2);
            
            // Display value of counter cookie
            lblCounter.Text = counter.ToString();
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Set Persister Cookie</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        You have visited this page
        <asp:Label
            id="lblCounter"
            Runat = "server" />
        times!
        </div>
        </form>
    </body>
    </html>
  • 相关阅读:
    开发两年!JVM方法调用都玩不明白,你离被炒鱿鱼不远了!
    springboot基本框架搭建零基础教程,对新手极为友好!
    SpingBoot整合Mybatis,这些小技巧你得知道,对你工作有很大的帮助!
    今天我们基于jdk8聊聊JVM-常量池,希望对大家有帮助!
    剑指 Offer 12. 矩阵中的路径
    WUSTCTF2020 funnyre
    2020 DJBCTF RE wp
    黑马c++基础的一个通讯录系统
    elf文件结构解读以及plt节got节的理解
    ubuntu16.04上编译android的可执行文件并调用本地so库
  • 原文地址:https://www.cnblogs.com/hellolong/p/2771437.html
Copyright © 2011-2022 走看看