cookie 的書寫

  protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie MyCookie = Request.Cookies["User_location"];
        if (MyCookie == null || MyCookie.Value.Length <= 0)
        {
            string Location_txt = "王先生" + "|" + DateTime.Now.ToString();
            MyCookie= new HttpCookie("User_location", Location_txt);
            MyCookie.Expires.AddYears(100);
            Response.Cookies.Add(MyCookie);
        }
        else
        {
            Response.Write("現在登錄時間:" + DateTime.Now.ToString() + "<br/>");
            string[] Loca_txt = MyCookie.Value.Split(new char[] { '|' });
            Response.Write("用戶名:" + Loca_txt[0].ToString() + "<br/>");
            Response.Write("上次登錄時間:" + Loca_txt[1].ToString() + "<br/>");
            MyCookie.Value = Loca_txt[0].ToString() + "|" + DateTime.Now.ToString();
            Response.Cookies.Set(MyCookie);
            //Response.AppendCookie(MyCookie);同下
            string[] keysstr = Response.Cookies.AllKeys;
            Response.Write(keysstr[0]);
        }
    }

發佈了68 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章