Test the cookie length limit in different browsers

Source: Internet
Author: User
Tags set cookie

(1) the current page is not set with the same name; ()

(2) The cookie with the same name has been set on the current page. The main test is Code As follows:
 
// A super-long string exceeding 4 K

VaR longstring = 'dfsafdsafdfsafdsafdfsafdsafdsafdfsafdfsafdfs

Afdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfs

Afdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsaf

Dfsafdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdf

Safdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsafdfsafdfsafdsafdfs

Afdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsafdfsafdfsafdsafdsafdfsaf

Dsafdfsafdsafdfsafdsafdfsafdsafdsafdfsafdsafdfsafdfsafdsafdsafdfsafdsafd

Fsafdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsafdfsafdfsafdsafdsafdfsa

Fdsafdfsafdsafdfsafdsafdfsafdsafdsafdfsafdsafdfsafdfsafdsafdsafdfsafdsafdf

Bytes

Bytes

Bytes

Bytes

Bytes

Dfsafdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsafdfsafdfsaf

Dsafdfsa

Fdsafdfsafdsafdfsafdsafdfsafdsafdsafdfsafdsafdfsafdfsafdsafdsafdfsa

Fdsafdfsafdsafdfsafdsafdfsafdsafdsafdfsafdsafdfsafdfsafdsafdsafdfsafdsaf

Bytes

;

// Write cookie Function

Function setcookie (name, value)
{
VaR days = 1; // This cookie will be saved for 1 day
VaR exp = new date ();
Exp. settime (exp. gettime () + days x 24x60*60*1000 );
Document. Cookie = Name + "=" + escape (value) + "; expires =" + exp. togmtstring ();
}

Function getcookie (name) // The cookie function.
{
VaR arr = Document. Cookie. Match (New Regexp ("(^ |)" + name + "= ([^;] *) (; | $ )"));
If (Arr! = NULL) return Unescape (ARR [2]); return NULL;
}

// Set the cookie, which containsArticleTwo situations mentioned at the beginning:
Setcookie ('test', 'cookie already set ');

// Set the ultra-long COOKIE:
Setcookie ('test', longstring );

Alert (getcookie ('test '));

The test results are as follows:

(1) No cookie of the same name is set on the current page;

browser version output result
IE6 null
IE7 null
IE8 null
firefox null
chrome null
opera null
Conclusion: Writing strings larger than 4 kb in browsers of the above versions (of course, different browsers may have different situations, some are 4095 bytes, some are 4096 bytes, the write truncation cannot be performed. (2) The cookie with the same name has been set on the current page;
browser version output result
IE6 null
IE7 null
IE8 set cookie
firefox set cookie
chrome set cookie
opera set cookie
Conclusion: If a value named 'test' already exists in browsers of the above versions, the data written exceeds 4 kb (of course, the situation may vary with browsers, some are 4095 bytes, and some are 4096 bytes) string: 1. in IE6 and IE7, when a super-long string cannot be written, the cookie value with the original key 'test' is affected. In the test results, the cookie value is cleared. 2. in other browsers, the cookie value of the original same key name is not affected when the ultra-long string cannot be written. The original cookie value can still be read normally.

(1) the current page is not set with the same name; ()

(2) The cookie with the same name has been set on the current page. The main test code is as follows:
 
// A super-long string exceeding 4 K

VaR longstring = 'dfsafdsafdfsafdsafdfsafdsafdsafdfsafdfsafdfs

Afdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfs

Afdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsaf

Dfsafdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdf

Safdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsafdfsafdfsafdsafdfs

Afdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsafdfsafdfsafdsafdsafdfsaf

Dsafdfsafdsafdfsafdsafdfsafdsafdsafdfsafdsafdfsafdfsafdsafdsafdfsafdsafd

Fsafdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsafdfsafdfsafdsafdsafdfsa

Fdsafdfsafdsafdfsafdsafdfsafdsafdsafdfsafdsafdfsafdfsafdsafdsafdfsafdsafdf

Bytes

Bytes

Bytes

Bytes

Bytes

Dfsafdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsafdfsafdfsaf

Dsafdfsa

Fdsafdfsafdsafdfsafdsafdfsafdsafdsafdfsafdsafdfsafdfsafdsafdsafdfsa

Fdsafdfsafdsafdfsafdsafdfsafdsafdsafdfsafdsafdfsafdfsafdsafdsafdfsafdsaf

Bytes

;

// Write cookie Function

Function setcookie (name, value)
{
VaR days = 1; // This cookie will be saved for 1 day
VaR exp = new date ();
Exp. settime (exp. gettime () + days x 24x60*60*1000 );
Document. Cookie = Name + "=" + escape (value) + "; expires =" + exp. togmtstring ();
}

Function getcookie (name) // The cookie function.
{
VaR arr = Document. Cookie. Match (New Regexp ("(^ |)" + name + "= ([^;] *) (; | $ )"));
If (Arr! = NULL) return Unescape (ARR [2]); return NULL;
}

// Set the cookie, which contains the two situations mentioned at the beginning of the article:
Setcookie ('test', 'cookie already set ');

// Set the ultra-long COOKIE:
Setcookie ('test', longstring );

Alert (getcookie ('test '));

The test results are as follows:

(1) No cookie of the same name is set on the current page;

browser version output result
IE6 null
IE7 null
IE8 null
firefox null
chrome null
opera null
Conclusion: Writing strings larger than 4 kb in browsers of the above versions (of course, different browsers may have different situations, some are 4095 bytes, some are 4096 bytes, the write truncation cannot be performed. (2) The cookie with the same name has been set on the current page;
browser version output result
IE6 null
IE7 null
IE8 set cookie
firefox set cookie
chrome set cookie
opera set cookie
Conclusion: If a value named 'test' already exists in browsers of the above versions, the data written exceeds 4 kb (of course, the situation may vary with browsers, some are 4095 bytes, and some are 4096 bytes) string: 1. in IE6 and IE7, when a super-long string cannot be written, the cookie value with the original key 'test' is affected. In the test results, the cookie value is cleared. 2. in other browsers, the cookie value of the original same key name is not affected when the ultra-long string cannot be written. The original cookie value can still be read normally.

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.