JS set cookies, and restrictions on cookies

Source: Internet
Author: User

In JavaScript scripts, a cookie is actually a string attribute. When you read the value of a cookie, you get a string that contains the names and values of all the cookies used in the current Web page. Each cookie has four properties in addition to the name and value values. These properties are: Expires expiration time, path path, domain realm, and secure security.

expires– Expiration time. Specifies the lifetime of the cookie. The specific value is the expiration date. You must use this property if you want the cookie to exist longer than the current browser session. When the expiration date is over, the browser can delete the cookie file without any effect.

path– path. Specifies the Web page that is associated with the cookie. The value can be a directory, or a path. If http://www.zdnet.com/devhead/index.html establishes a cookie, then all pages in the http://www.zdnet.com/devhead/directory, and the pages in any subdirectory below the directory can access this cookie. This means that any page in Http://www.zdnet.com/devhead/stories/articles can access the cookie created by http://www.zdnet.com/devhead/index.html. However, what if http://www.zdnet.com/zdnn/needs to access the Cookes of the http://www.zdnet.com/devhead/index.html settings? At this point, we want to set the path property of the cookie to "/". When specifying a path, cookies can be shared by all Web pages that have the same path in the URL from the same server. Now let's look at another example: if you want http://www.zdnet.com/devhead/filters/and http://www.zdnet.com/devhead/stories/to share cookies, set path to "/ Devhead ".

domain– domain. Specifies the associated Web server or domain. The value is a domain name, such as Zdnet.com. This is an extension to the Path property. What if we want catalog.mycompany.com to have access to cookies set by shoppingcart.mycompany.com? We can set the domain property to "mycompany.com" and set the Path property to "/". The cookie domain attribute cannot be set to a different value than the domain of the server where it is set.

secure– safe. Specifies how the value of the cookie is passed between the user and the Web server through the network. The value of this property is either "secure", or null. By default, this property is empty, which means that data is passed using an unsecured HTTP connection. If a cookie is marked secure, it passes data to and from the Web server over HTTPS or other security protocols. However, setting the secure property does not mean that others cannot see the cookie that is stored locally on your machine. In other words, setting the cookie to secure only guarantees that the data transfer process between the cookie and the Web server is encrypted, while the cookie file stored locally is not encrypted. If you want to encrypt your local cookie, you have to encrypt your data yourself.

JS How to set Cookies

function Setcookie (c_name,value,expiredays) {
var exdate=new Date ();
Exdate.setdate (Exdate.getdate () +expiredays);
document.cookie=c_name+ "=" +escape (value) +
((expiredays==null)? "": "; expires=" +exdate.togmtstring ()) + ";p ath=/";
}
function GetCookie (c_name) {
if (document.cookie.length>0)
{
C_start=document.cookie.indexof (c_name + "=");
if (c_start!=-1)
{
C_start=c_start + c_name.length+1;
C_end=document.cookie.indexof (";", C_start);
if (c_end==-1) c_end=document.cookie.length;
Return unescape (document.cookie.substring (c_start,c_end));
}
}
Return "";
}

First, the browser allows each domain name contains the number of cookies: Microsoft noted that Internet Explorer 8 added a cookie limit of 50 per domain name, but IE7 also seemed to allow 50 cookies per domain name (color= "#006d A3 "> Update to Internet explorer’s Cookie Jar"). Firefox has a limit of 50 cookies per domain name. Opera has a limit of 30 cookies per domain name. Safari/webkit seems to have no cookie restrictions. However, if there are many cookies, the header size will exceed the server's processing limit, which can cause errors to occur. Note: Each domain name cookie limit of 20 will no longer be correct! Second, when a lot of cookies are set, how the browser responds. In addition (can set all cookies, regardless of quantity), there are two methods: least recently Used (least recently Used (LRU)) method: When the cookie has reached the limit, automatically kicks apart the oldest cookie to give some space to the latest cookie. Internet Explorer and Opera Use this method. Firefox is unique: Although the last set of cookies is always retained, it seems to randomly determine which cookies are retained. There seems to be no plan (recommended: Do not exceed the Cookie limit in Firefox). Third, the total size of the cookie between different browsers is also different: Firefox and Safari allow up to 4,097 bytes of cookies, including the name, value, and equal sign. Opera allows up to 4,096 bytes of cookies, including: Name, value, and equal sign. Internet Explorer allows up to 4,095 bytes of cookies, including: Name, value, and equal sign. Note: Multibyte characters are calculated as two bytes. In all browsers, any cookie size exceeding the limit is ignored and will never be set.

JS setting cookies, and restrictions on cookies

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.