Home  >  Article  >  Web Front-end  >  Three ways to operate cookies

Three ways to operate cookies

黄舟
黄舟Original
2016-12-16 10:50:591151browse

1, jquery.cookie.js

This article has been written in great detail:

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

2, When using native js to operate cookies

jquery.cookie.js, there will usually be cookie incompatibility issues, so you need to use native js

3, server End processing

///


/// Store the click value in the cookie
///

PRivate void SetCookieValue(HttpContext context, string dic, string cookieName, string keyName)
{
                                                   HttpCookie Cookie = New HttpCookie (cookiename); // The name of the cookies and set the name of the cookie
if (context.request.cookies [cookiename]! = Null) {
cookie = context.request.coo okies [cookiename];
IF ( cookie.Values.Count > 0 && cookie.Values.GetValues(keyName) != null && cookie.Values.GetValues(keyName).Length > 0)
                                                                                                                                                                                                    }
                                 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);
}
///
/// Get the clicked value out of the cookie
///

public string GetClickValue(HttpContext context, string cookieName, string keyName)
{
string userName = "";
if (context.Request.Cookies[cookieName] != null)
                                                                                                       (context.Request.Cookies[cookieName][keyName] != null)
                                                                                                                            }
                             //userName = NCookieUtil.GetCookie( keyName);
            return userName; void Remove(string cookieName, string keyName)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie != null)
{
if (cookie.Values.Count > 0)
                                                 
                                                                                                                                                                                                                                                   Cookie.Values.Remove(HttpUtility.UrlEncode(keyName));
cookie.Expires = DateTime.Now.AddDays (-1); HttpUtility.UrlEncode(keyName));
                                                                                                                             HttpUtility. ;
}
}}}

In fact, these three methods are still recommended to use the second method!

The above is the content of the three operating methods of cookies. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn