ASP (UTF-8) set cookie reads a cookie with JavaScript and discovers a problem

Source: Internet
Author: User
Tags set cookie setcookie

Asp:

<%setcookie ("user_id", "Test")%>//Pass a cookie, name: user_id value: Test

Javascript:

<script>
Alert (GetCookie ("user_id")); Read results show null Halo!
</script>

Use Firefox to view cookies and find that cookies are a bit different.

Cookie name: user_id changed to User%5fid

Why does the name change? I went to Baidu search, find the reason:
Http://bbs.chinaunix.net/thread-743431-1-1.html

It turns out that when an ASP uses UTF-8 encoding, the cookie written to the browser is UrlEncode encoded.

The solution is to decode in javascript: decodeuricomponent

Modified JavaScript code:

Set a cookie with the name named value
function Setcookie (name, value)
{
var argc = setCookie.arguments.length;
var argv = setcookie.arguments;
var path = (argc > 3)? ARGV[3]: null;
var domain = (argc > 4)? ARGV[4]: null;
var secure = (argc > 5)? ARGV[5]: false;
Document.cookie = name + "=" + value + (path = = null)? "" : ("; Path= "+ path") + ((domain = = null)? "" : ("; domain= "+ domain") + ((secure = = True)? "; Secure ":");
}

Delete a cookie named name
function Deletecookie (name)
{
var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var cval = GetCookie (name);
Document.cookie = name + "=" + Cval + "; Expires= "+ exp.togmtstring ();
}

Gets the value of the cookie that is called offset by the item name
function Getcookieval (offset)
{
var cookie_content = decodeuricomponent (Document.cookie);
var endstr = Cookie_content.indexof (";", offset);
if (endstr = =-1) endstr = cookie_content.length;                               Return unescape (cookie_content.substring (offset, endstr)); }

Get the cookie value named name
function GetCookie (name)
{
var arg = name + "=";
var alen = Arg.length;
var cookie_content = decodeuricomponent (Document.cookie);
var clen = cookie_content.length;
var i = 0;
while (I < Clen)
{
var j = i + Alen;
if (Cookie_content.substring (i, j) = = arg)
Return Getcookieval (j);
i = Cookie_content.indexof ("", I) + 1;
if (i = = 0) break;
}
return null;
}

ASP (UTF-8) set cookie reads a cookie with JavaScript and discovers a problem

Related Article

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.