Three methods of cookie operation: cookie operation

Source: Internet
Author: User

Three methods of cookie operation: cookie operation

1. jquery. cookie. js

This article has been written in detail:

Http://www.cnblogs.com/afuge/archive/2013/07/03/3169048.html

2. Native js operation cookie

Jquery. cookie. js generally causes cookie incompatibility issues and requires native js.

<script language=javascript>   // Obtain the coolie Value function cookie(name){        var cookieArray=document.cookie.split( "; " ); // Obtain the split cookie name value pair     var cookie= new Object();        for (var i= 0 ;i<cookieArray.length;i++){           var arr=cookieArray[i].split( "=" );       // Separate names and values        if (arr[ 0 ]==name) return unescape(arr[ 1 ]); // If the cookie is specified, its value is returned.           return "" ; }   function delCookie(name) // Delete the cookie {     document.cookie = name+ "=;expires=" +( new Date( 0 )).toGMTString(); }  function getCookie(objName){ // Obtain the cookie value of the specified name      var arrStr = document.cookie.split( "; " );      for (var i = 0 ;i < arrStr.length;i ++){       var temp = arrStr[i].split( "=" );          if (temp[ 0 ] == objName) return unescape(temp[ 1 ]);     } }  function addCookie(objName,objValue,objHours){      // Add cookie      var str = objName + "=" + escape(objValue);      if (objHours > 0 ){                               // The expiration time is not set for the last time. When the browser is closed, the cookie will automatically disappear.          var date = new Date();          var ms = objHours* 3600 * 1000 ;          date.setTime(date.getTime() + ms);          str += "; expires=" + date.toGMTString();     }     document.cookie = str; }  function SetCookie(name,value) // Two parameters: one is the cookie name and the other is the value. {       var Days = 30 ; // This cookie will be saved for 30 days      var exp = new Date();    //new Date("December 31, 9998");      exp.setTime(exp.getTime() + Days* 24 * 60 * 60 * 1000 );      document.cookie = name + "=" + escape (value) + ";expires=" + exp.toGMTString(); }  function getCookie(name) // Cookies Function {      var arr = document.cookie.match( new RegExp( "(^| )" +name+ "=([^;]*)(;|$)" ));       if (arr != null ) return unescape(arr[ 2 ]); return null ; }  function delCookie(name) // Delete the cookie {      var exp = new Date();      exp.setTime(exp.getTime() - 1 );      var cval=getCookie(name);      if (cval!= null ) document.cookie= name + "=" +cval+ ";expires=" +exp.toGMTString(); } </script>

3. server-side Processing

/// <Summary>
/// Store the clicked value in the Cookie
/// </Summary>
Private void SetCookieValue (HttpContext context, string dic, string cookieName, string keyName)
{
HttpCookie cookie = new HttpCookie (cookieName); // initialize and set the Cookie name
If (context. Request. Cookies [cookieName]! = Null)
{
Cookie = context. Request. Cookies [cookieName];
If (cookie. Values. Count> 0 & cookie. Values. GetValues (keyName )! = Null & cookie. Values. GetValues (keyName). Length> 0)
{
Remove (cookieName, keyName );
}
}
TimeSpan ts = new TimeSpan (0, 3, 0, 0, 0); // The expiration time is 3 hours.
Cookie. Expires = DateTime. Now. Add (ts); // set the expiration time
Cookie. Values. Add (keyName, dic );
Context. Response. AppendCookie (cookie );
// NCookieUtil. SetCookie (keyName, dic );
}
/// <Summary>
/// Extract the clicked value from the Cookie
/// </Summary>
Public string GetClickValue (HttpContext context, string cookieName, string keyName)
{
String userName = "";
If (context. Request. Cookies [cookieName]! = Null)
{
If (context. Request. Cookies [cookieName] [keyName]! = Null)
{
UserName = context. Request. Cookies [cookieName] [keyName]. ToString ();
}
}
// UserName = NCookieUtil. GetCookie (keyName );
Return userName;
}
/// <Summary>
/// Remove the key specified in the cookie. If the last key is used, the cookie is removed.
/// </Summary>
Public static void Remove (string cookieName, string keyName)
{
HttpCookie cookie = HttpContext. Current. Request. Cookies [cookieName];
If (cookie! = Null)
{
If (cookie. Values. Count> 0)
{
If (cookie. Values. Count = 1)
{
// If the last key is used, the cookie is removed; otherwise, a null value is added.
Cookie. Values. Remove (HttpUtility. UrlEncode (keyName ));
Cookie. Expires = DateTime. Now. AddDays (-1 );
}
Else
{
Cookie. Values. Remove (HttpUtility. UrlEncode (keyName ));
}
HttpContext. Current. Response. Cookies. Add (cookie );
}
}
}

In fact, the second method is recommended for these three methods!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.