Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall
I have been involved in the development of a large multi-user mall, a lot of system user role, there are buyers, sellers, agents, system administrators, ordinary administrators, super administrators, etc., these users are involved in the landing system, and then create the problem of cookies, because of the different roles, these users login system, The cookies are relatively independent, and for a multi-user mall, cookies are important to differentiate domains, different domain names to build different domains. So the author in the process of development, wrote many times to build cookies, take cookies code, annoying!
Since all are to build cookies, then there must be something in common, why don't we abstract out, write a generic cookie operation class, the following I take. NET as an example, to explain how to implement this class!
Here is the function to create cookies
public void Setcookies (string domain, String type, Xiegou.xgmap map)
{
HttpCookie cookie = new HttpCookie (type);//define Cookie Object
DateTime dt = datetime.now;//Definition Time Object
TimeSpan ts=new TimeSpan (1,0,0,0)//cookie effective time, specifically check MSDN
Cookie. Expires = dt. Add (TS);
Cookie. domain = domain;
for (int i =0 i < map. Size (); i++)
{
Cookie. Values.add (map. Gettag (i), map. GetValue (i). ToString ());
}
Page. Response.appendcookie (cookie);
}
Here is the function to take cookies
public string GetCookies (string type, string name)
{
if (page. Request.cookies[type]!= null)
{
if (page. Request.cookies[type]. Values[name]!= null)
{
Return page. Request.cookies[type]. Values[name]. ToString ();
}
Else
{
return "0";
}
}
return "0";
}
Let's look at how to call these two functions:
Xiegou.xgmap map = new Xgmap (); The map class used here can refer to the online data to write a
Map. ADD ("Shopid", shop.m_id. ToString ()); Add map corresponding table
Map. ADD ("Shopname", Shop.m_name. ToString ());
Func. Setcookies (". shopxg.com", "Shopxg_namespace", map);
The principle is very simple, first define a map class, the name and value of the cookies to be built in the map table, and then unified by the setcookies to build cookies, the advantage of this is that you can write a lot of code, especially the need to frequently build cookies site!
Above is the author's humble opinion, if there is a problem, welcome to exchange. Email:cxl@xiegoo.com.