购物车之服务器端操作Cookie 实例
2007-11-14 13:57
585 查看
public class Cookie
{
public static readonly string strcolumen = " or bkpzid='";
public static string readCookie()
{
string coo = "";
string sql = "";
if (HttpContext.Current.Request.Cookies["dyid"] != null)
{
coo = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies["dyid"].Value);
}
string[] varstr = coo.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < varstr.Length; i++)
{
if (i == 0)
{
sql = varstr[i].ToString() + "'";
}
else
{
sql += strcolumen + varstr[i].ToString() + "' ";
}
}
return sql;
}
public static void writeCookie(string id)
{
//判断是否存在Cookie
bool flag = false;
string coo = "";
//判断Coolie在硬盘上是否存在,存在就用request 取出Cookie,不存在就创建Cookie
if (HttpContext.Current.Request.Cookies["dyid"] != null)
{
coo = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies["dyid"].Value);
string[] varstr = coo.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
//判断Coolie的值是否为空字符串,做特殊处理
if (coo == "")
{
flag = true;
}
for (int i = 0; i < varstr.Length; i++)
{
if (id.ToUpper() == varstr[i].ToString())
{
//存在
flag = false;
break;
}
else
{
flag = true;
}
}
if (flag)
{
coo = coo + id + ",";
HttpCookie dycookie = HttpContext.Current.Request.Cookies["dyid"];
dycookie.Value = HttpContext.Current.Server.UrlEncode(coo);
dycookie.Expires = System.DateTime.Now.AddDays(7);
HttpContext.Current.Response.Cookies.Add(dycookie);
}
}
else
{
coo = id + ",";
HttpCookie dycookie = new HttpCookie("dyid");
dycookie.Value = HttpContext.Current.Server.UrlEncode(coo);
dycookie.Expires = System.DateTime.Now.AddDays(7);
HttpContext.Current.Response.Cookies.Add(dycookie);
}
}
/// <summary>
/// 针对 CLOSE 按钮删除COOKIE
/// </summary>
/// <param name="id"></param>
public static string updateCookie(string id)
{
string coo = "";
string value = "";
if(HttpContext.Current.Request.Cookies["dyid"] != null)
{
coo = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies["dyid"].Value);
}
string[] varstr = coo.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < varstr.Length; i++)
{
if (id.ToUpper() == varstr[i].ToString())
{
i++;
}
if (i < varstr.Length)
{
value += varstr[i] + ",";
}
}
HttpCookie dycookie = HttpContext.Current.Request.Cookies["dyid"];
dycookie.Value = HttpContext.Current.Server.UrlEncode(value);
dycookie.Expires = System.DateTime.Now.AddDays(7);
HttpContext.Current.Response.Cookies.Add(dycookie);
return readCookie();
}
public static void deleteCookie()
{
//删除Cookie 与创建 Cookie的方法相同,只需要把时间设置为过去某一个时刻。
HttpCookie dycookie = new HttpCookie("dyid");
dycookie.Value = "1";
dycookie.Expires = System.DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(dycookie);
}
}
{
public static readonly string strcolumen = " or bkpzid='";
public static string readCookie()
{
string coo = "";
string sql = "";
if (HttpContext.Current.Request.Cookies["dyid"] != null)
{
coo = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies["dyid"].Value);
}
string[] varstr = coo.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < varstr.Length; i++)
{
if (i == 0)
{
sql = varstr[i].ToString() + "'";
}
else
{
sql += strcolumen + varstr[i].ToString() + "' ";
}
}
return sql;
}
public static void writeCookie(string id)
{
//判断是否存在Cookie
bool flag = false;
string coo = "";
//判断Coolie在硬盘上是否存在,存在就用request 取出Cookie,不存在就创建Cookie
if (HttpContext.Current.Request.Cookies["dyid"] != null)
{
coo = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies["dyid"].Value);
string[] varstr = coo.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
//判断Coolie的值是否为空字符串,做特殊处理
if (coo == "")
{
flag = true;
}
for (int i = 0; i < varstr.Length; i++)
{
if (id.ToUpper() == varstr[i].ToString())
{
//存在
flag = false;
break;
}
else
{
flag = true;
}
}
if (flag)
{
coo = coo + id + ",";
HttpCookie dycookie = HttpContext.Current.Request.Cookies["dyid"];
dycookie.Value = HttpContext.Current.Server.UrlEncode(coo);
dycookie.Expires = System.DateTime.Now.AddDays(7);
HttpContext.Current.Response.Cookies.Add(dycookie);
}
}
else
{
coo = id + ",";
HttpCookie dycookie = new HttpCookie("dyid");
dycookie.Value = HttpContext.Current.Server.UrlEncode(coo);
dycookie.Expires = System.DateTime.Now.AddDays(7);
HttpContext.Current.Response.Cookies.Add(dycookie);
}
}
/// <summary>
/// 针对 CLOSE 按钮删除COOKIE
/// </summary>
/// <param name="id"></param>
public static string updateCookie(string id)
{
string coo = "";
string value = "";
if(HttpContext.Current.Request.Cookies["dyid"] != null)
{
coo = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies["dyid"].Value);
}
string[] varstr = coo.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < varstr.Length; i++)
{
if (id.ToUpper() == varstr[i].ToString())
{
i++;
}
if (i < varstr.Length)
{
value += varstr[i] + ",";
}
}
HttpCookie dycookie = HttpContext.Current.Request.Cookies["dyid"];
dycookie.Value = HttpContext.Current.Server.UrlEncode(value);
dycookie.Expires = System.DateTime.Now.AddDays(7);
HttpContext.Current.Response.Cookies.Add(dycookie);
return readCookie();
}
public static void deleteCookie()
{
//删除Cookie 与创建 Cookie的方法相同,只需要把时间设置为过去某一个时刻。
HttpCookie dycookie = new HttpCookie("dyid");
dycookie.Value = "1";
dycookie.Expires = System.DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(dycookie);
}
}
相关文章推荐
- 购物车之服务器端操作Cookie 实例
- jquery.cookie实现的客户端购物车操作实例
- 在Vue中如何使用Cookie操作实例
- JS操作Cookie写入和读取实例代码
- php针对cookie操作的队列操作类实例
- JavaScript操作Cookie方法实例分析
- JS封装cookie操作函数实例(设置、读取、删除)
- asp.net中购物车的两种存储方式Session和Cookie的应用实例
- [网络收集]asp.net中购物车的两种存储方式Session和Cookie的应用实例
- 服务器端操作Cookie[2]
- php中cookie实现二级域名可访问操作实例
- asp.net中购物车的两种存储方式Session和Cookie的应用实例
- jQuery基于cookie实现的购物车实例分析
- jQuery操作cookie方法实例教程
- JavaScript中Cookie操作实例
- javascript针对cookie的基本操作实例详解
- JS操作Cookie写入和读取实例代码
- JS操作cookie的实例
- yii操作cookie实例简介