Test the Cookie length limit in different browsers

Source: Internet
Author: User
Tags setcookie

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

(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:
In the browsers of the above versions, writing strings larger than 4 KB (of course, different browsers may have different situations, some are 4095 bytes, some are 4096 bytes) will fail to be written,
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
Cookie already set
Firefox
Cookie already set
Chrome
Cookie already set
Opera
Cookie already set
Conclusion:
When a value named 'test' already exists in browsers of the above versions, it is written to a string larger than 4 KB (of course, the situation may be different in different browsers, some are 4095 bytes, some are 4096 bytes:
1. In ie6 and ie7, if a super-long string cannot be written, the cookie value with the original key 'test' will be 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.