JS Operation Cookie Cookie path problem

Source: Internet
Author: User

Here is not the main way to talk about this method, JS write cookies This code on the Internet, when the use of a bit of a problem, is to write a cookie is a path problem, in the user directory can be used to jump to another directory under the cookie, After comparing the differences between the cookie and other cookies found, the cookie is written in the current directory, not the root directory, here can be used to control access but not meet the current needs, somewhere on the internet to see a piece of code, can solve this problem, posted here, We need to use for reference. The code is as follows:

1Utils.writecookie =function(value, name, key) {2 varDays = 2;3 varExp =NewDate ();4Exp.settime (Exp.gettime () + days * 24 * 60 * 60 * 1000);5 if(Key = =NULL|| Key = = "") {6Document.cookie = name + "=" + encodeURI (value) + "; expires=" + exp.togmtstring () + ";p ath=/";7 }8 Else {9 varNamevalue =GetCookie (name);Ten       if(Namevalue = = "") { OneDocument.cookie = name + "=" + key + "=" + encodeURI (value) + "; expires=" + exp.togmtstring () + ";p ath=/"; A       } -       Else { -           varKeyValue =GetCookie (name, key); the           if(KeyValue! = "") { -Namevalue = namevalue.replace (key + "=" + keyValue, key + "=" +encodeURI (value)); -Document.cookie = name + "=" + Namevalue + "; expires=" + exp.togmtstring () + ";p ath=/"; -           } + Else {  -Document.cookie = name + "=" + Namevalue + "&" + key + "=" + encodeURI (value) + "expires=" + exp.togmtstring () + ";p A th=/"; + } A }  at } -}

Incidentally, the reading of the cookie has been affixed together, citing the other person's code is not original:

1Utils.readcookie =function(name,key) {2 varNamevalue = "";3 vararr,reg=NewRegExp ("(^|)" +name+ "= ([^;] *)(;|$)");4 if(arr =Document.cookie.match (REG)) {5Namevalue = decodeURI (arr[2]);6 }7 if(Key! =NULL&& Key! = "") {8Reg =NewRegExp ("(^| |&)" + key + "= ([^ (; |&|=)]*) (&|$)");9 if(arr =Namevalue.match (REG)) {TenAlert (decodeURI (arr[2));returndecodeURI (arr[2]); One } A Else return""; - } - Else { the returnNamevalue; - } -}

JS Operation Cookie Cookie path problem

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.