zoukankan      html  css  js  c++  java
  • asp.net 操作 cookie

     protected void Button1_Click(object sender, EventArgs e) //写入Cookie
        {
            HttpCookie cookie 
    = new HttpCookie("Info");//定义cookie对象以及名为Info的项
            DateTime dt = DateTime.Now;//定义时间对象
            TimeSpan ts = new TimeSpan(1000);//cookie有效作用时间,具体查msdn
            cookie.Expires = dt.Add(ts);//添加作用时间
            cookie.Values.Add("user""cxbkkk");//增加属性
            cookie.Values.Add("userid""1203");
            Response.AppendCookie(cookie);
    //确定写入cookie中
        }
        
    protected void Button2_Click(object sender, EventArgs e)//读取Cookie
        {
            
    if (Request.Cookies["Info"!= null)
            {
                
    string temp = Convert.ToString(Request.Cookies["Info"].Values["user"]) + "  " + Convert.ToString(Request.Cookies["Info"].Values["userid"]);
                
    //读全部就用Request.Cookies["Info"].Value)
                if (temp == "")
                {
                    Response.Write(
    "");
                }
                
    else
                    Response.Write(temp);
            }
            
    else
            {
                Response.Write(
    "无cookie");
            }
        }
        
    protected void Button3_Click(object sender, EventArgs e) //修改Cookie
        {
            Response.Cookies[
    "Info"]["user"= "2";
            Response.Cookies[
    "Info"].Expires = DateTime.Now.AddDays(1);
        }
        
    protected void Button4_Click(object sender, EventArgs e)//删除Cookie
        {
            
    // 删除cookie下的属性
            HttpCookie acookie = Request.Cookies["Info"];
            acookie.Values.Remove(
    "userid");
            acookie.Expires 
    = DateTime.Now.AddDays(1);
            Response.Cookies.Add(acookie);    
            
    //删除所有cookie,就是设置过期时间为现在就行了
            int limit = Request.Cookies.Count - 1;
            
    for (int i = 0; i < limit; i++)
            {
                acookie 
    = Request.Cookies[i];
                acookie.Expires 
    = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(acookie);
            }       
    // 这下不用到处找了 
        }




    每个页面下面放以下代码
    <script src="http://www.cnblogs.com/clicktotal.aspx?proid=247"></script>

    //写入cookie  点击率
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class clicktotal : System.Web.UI.Page
    {
        
    protected void Page_Load(object sender, EventArgs e)
        {
            
            
    this.clickfun();
        }
        
    //写入cookie
        private void writeCookie(string strId)
        {
            
    if (Request.Cookies["pp"!= null)
            {
                
    string temp = Convert.ToString(Request.Cookies["pp"].Values["id"]);
                
    if (temp.IndexOf(strId) >-1)//判断是否出现过
                {
                    temp 
    = temp.Replace(strId, "");
                    temp 
    += strId + "";
                }
                
    else
                {
                    temp 
    += strId + "";
                }
                
                Response.Cookies[
    "pp"]["id"= temp;
                Response.Cookies[
    "pp"].Expires = DateTime.Now.AddDays(1);

            }
            
    else
            {
                HttpCookie cookie 
    = new HttpCookie("pp");//定义cookie对象以及名为Info的项
                DateTime dt = DateTime.Now;//定义时间对象
                TimeSpan ts = new TimeSpan(1000);//cookie有效作用时间,具体查msdn
                cookie.Expires = dt.Add(ts);//添加作用时间
                cookie.Values.Add("id", strId);//增加属性
                Response.AppendCookie(cookie);//确定写入cookie中
            }
        }
       
    private void clickfun()//获取商品点击率
       {
            
    if (Request["proid"!= null)
            {
                
    string strId = Request["proid"];
                
    string strSql = "select top 1 * from product where id="+strId;
                db d 
    = new db();
                DataTable dt 
    = d.getDT(strSql);
                
    int inTotal = 0;
                
    if (dt.Rows.Count > 0)
                {
                    
    if (dt.Rows[0]["totalHits"].ToString() != "")
                    {
                        inTotal 
    = Convert.ToInt32(dt.Rows[0]["totalHits"]) + 1;
                    }
                    
    else
                    {
                        inTotal 
    = 1;
                    }
                    strSql 
    = "update product set totalHits="+inTotal+" where id="+strId;
                    d 
    = new db();
                    
    int i=d.excuteSQL(strSql);
                }
                
    //设置cookie
                this.writeCookie(strId+"|");
                
    //设置cookie结束
            }
            
            Response.Write(
    "document.write(\"\")");
       }
    }


    //获取cookie值
        private string getcookie()
        {
            
    string strReturn = "";
            
    if (Request.Cookies["pp"!= null)
            {
                
    string temp = Convert.ToString(Request.Cookies["pp"].Values["id"]);
                
    //读全部就用Request.Cookies["Info"].Value)
                if (temp != "")
                {
                    strReturn 
    = temp;
                }
            }
            
    return strReturn;
        }


       
    string str = this.getcookie();//获取cookie值
           
    // Response.Write(""+str+"");
           Response.Write( this.cchtml(str) );
        }
        
    //生成html并返回
        private string cchtml(string str)
        {
            
    string strReturn="";
            
    if (str != "")
            {
                
    string strShow = "<div id='left3'><div class='span'>你最近浏览的商品</div>";
                strShow 
    += "<dl>";
                
    string[] arra = str.Split('|');//截取内字符
                
    //for (int i = 0; i < arra.Length; i++)

                
    if (arra.Length < 6)
                {


                    
    for (int i = arra.Length - 1; i >= 0; i--)
                    {
                        
    if (arra[i] != "")
                        {
                            
    string strSql = "select * from product where id=" + arra[i].ToString();
                            db d 
    = new db();
                            DataTable dt 
    = d.getDT(strSql);
                            
    if (dt.Rows.Count > 0)
                            {
                                
    // strShow += "<dt><a href='" + this.strRoot(arra[i].ToString()) + "/" + dt.Rows[0]["proFname"] + ".htm'><img src='" + webUrl + "/img/produce.gif' alt='" + dt.Rows[0]["proCname"] + "' border='0' /></a></dt>";
                                strShow += "<dd><a href='" + this.strRoot(arra[i].ToString()) + "/" + dt.Rows[0]["proFname"+ ".htm'>" + dt.Rows[0]["proCname"+ "</a></dd>";
                            }
                        }

                    }
                }

                
    else
                {
                    
    int i, j;
                    
    for (i = 0; i < arra.Length; i++)
                        
    for (j = i + 1; j < arra.Length; j++)
                        {
                            
    string t = arra[i];
                            arra[i] 
    = arra[j];
                            arra[j] 
    = t;
                        }
                    
    for (i = 0; i < 6; i++)
                    {


                        
    if (arra[i] != "")
                        {
                            
    string strSql = "select * from product where id=" + arra[i].ToString();
                            db d 
    = new db();
                            DataTable dt 
    = d.getDT(strSql);
                            
    if (dt.Rows.Count > 0)
                            {

                                strShow += "<dd><a href='" + this.strRoot(arra[i].ToString()) + "/" + dt.Rows[0]["proFname"+ ".htm'>" + dt.Rows[0]["proCname"+ "</a></dd>";
                            }
                        }

                    }
                }
                
                strShow 
    += "</dl>";
                strShow 
    += "</div>";
                strReturn 
    = strShow;
            }
            
    else
            {
                
    string strShow = "<div id='left3'><div class='span'>你最近浏览的商品</div><dl>";
                strShow 
    += "<dd>&nbsp;您暂时没有浏览任何产品或cookie未正确启用</dd>";
                strShow 
    += "</dl></div>";
                strReturn 
    = strShow;
            }
            
    return strReturn;
               
        }

     

    ///设置cookie
    function setCookie(NameOfCookie, value, expiredays) {
        var ExpireDate = new Date();
        ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
        document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "": "; expires=" + ExpireDate.toGMTString());
    }
    ///获取cookie值
    function getCookie(NameOfCookie) {
        if (document.cookie.length > 0) {
            begin = document.cookie.indexOf(NameOfCookie + "=");
            if (begin != -1) {
                begin += NameOfCookie.length + 1;
                end = document.cookie.indexOf(";", begin);
                if (end == -1) end = document.cookie.length;
                return unescape(document.cookie.substring(begin, end));
            }
        }
        return null;
    }
    ///删除cookie
    function delCookie(NameOfCookie) {
        if (getCookie(NameOfCookie)) {
            document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }

  • 相关阅读:
    DOM(九)使用DOM设置文本框
    DOM(八)使用DOM控制表单
    DOM(七)使用DOM控制表格
    Javascript制作伸缩的二级菜单
    Javascript屏蔽鼠标的右键的两种方法。
    DOM(六)事件类型
    DOM(五)事件对象
    DOM(四)事件流
    DOM(三)使用DOM + Css
    Javascript一个在页面内追加元素的小例子
  • 原文地址:https://www.cnblogs.com/zengxiangzhan/p/1576763.html
Copyright © 2011-2022 走看看