protected void Button1_Click(object sender, EventArgs e) //寫入Cookie
{
HttpCookie cookie = new HttpCookie("Info");//定義cookie對象以及名為Info的項
DateTime dt = DateTime.Now;//定義時間對象
TimeSpan ts = new TimeSpan(1, 0, 0, 0);//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 + "";
Response.Cookies["pp"]["id"] = temp;
Response.Cookies["pp"].Expires = DateTime.Now.AddDays(1);
HttpCookie cookie = new HttpCookie("pp");//定義cookie對象以及名為Info的項
DateTime dt = DateTime.Now;//定義時間對象
TimeSpan ts = new TimeSpan(1, 0, 0, 0);//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 (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>";
}
}
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++)
strShow += "</dl>";
strShow += "</div>";
strReturn = strShow;
string strShow = "<div id='left3'><div class='span'>你最近浏覽的商品</div><dl>";
strShow += "<dd>&nbsp;您暫時沒有浏覽任何産品或cookie未正确啟用</dd>";
strShow += "</dl></div>";
<a></a>
///設定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";
}
本文轉自曾祥展部落格園部落格,原文連結:http://www.cnblogs.com/zengxiangzhan/archive/2009/09/30/1576763.html,如需轉載請自行聯系原作者