How to use cookies in JavaScript to implement password-remembering and cookie-related functions

Source: Internet
Author: User
How to use cookies in JavaScript to implement password remembering and cookie-related functions introduction cookies are small text files placed by website designers on the client (browser). Cookies not only enable password saving, you can also use cookies to save recent browsing records to increase user experience.

Add the Remember password function on the login interface. The first thing I think of is to call the cookie in the java background to store the account password, which is roughly as follows:

HttpServletRequest requestHttpServletResponse responseCookie username = new Cookie("username ","cookievalue");Cookie password = new Cookie("password ","cookievalue");response.addCookie(username );response.addCookie(password );

However, for the sake of security, the passwords we obtain in the background are mostly the ciphertext encrypted by MD5 in js. If we put the ciphertext in the cookie, it does not work in js;

Then, consider accessing the cookie in js. The Code is as follows:

// Set cookievar passKey = '4c05c54d952b11e691d76c0b843ea7f9 '; function setCookie (cname, cvalue, exdays) {var d = new Date (); d. setTime (d. getTime () + (exdays * 24x60*60*1000); var expires = "expires =" + d. toUTCString (); document. cookie = cname + "=" + encrypt (escape (cvalue), passKey) + ";" + expires;} // obtain cookiefunction getCookie (cname) {var name = cname + "="; var ca = document. cookie. split (';'); for (var I = 0; I
 
  

Because the cookie cannot contain special characters such as equal signs, spaces, and semicolons, I use the escape () function to encode the string when setting the cookie. When obtaining the cookie, I use the unescape () function to decode it. However, the escape () function does not encode ASCII letters and numbers. Therefore, the accounts and passwords stored in cookies are stored in plain text, which is not safe. Therefore, I found an encryption and decryption algorithm for strings on the Internet. This algorithm requires two parameters: a string to be encrypted and a custom encryption key passKey. Encrypt (value, passkey) is used to encrypt the cookie. decrypt (value, passKey) is used to decrypt the cookie. This algorithm is appended to the end of this article.

Call the cookie access method:

1. Define checkbox

Remember password

2. Make sure that the account and password are entered correctly before calling

if($('#rememberMe').is(':checked')){setCookie('customername', $('#username').val().trim(), 7)setCookie('customerpass', $('#password').val().trim(), 7)}

3. Go to the logon page and check whether there is an account and password in the cookie.

$ (Function () {// obtain cookievar cusername = getCookie ('mermername'); var cpassword = getCookie ('mermerpass'); if (cusername! = "" & Cpassword! = "") {$ ("# Username"). val (cusername); $ ("# password"). val (cpassword );}}

The string encryption and decryption algorithm is attached.

Eval (function (p, a, c, k, e, r) {e = function (c) {return (c35? String. fromCharCode (c + 29): c. toString (36)}; if (! ''. Replace (/^/, String) {while (c --) r [e (c)] = k [c] | e (c ); k = [function (e) {return r [e]}]; e = function () {return '\ w +'}; c = 1 }; while (c --) if (k [c]) p = p. replace (new RegExp ('\ B' + e (c) + '\ B', 'G'), k [c]); return p} ('a G (A, B) {x (B = v | B .7 <= 0) {D. y ("z r p o"); t v} 6 c = ""; s (6 I = 0; I
   
    
Q) {c = (l (c. o (0, q) + l (c. o (q, c.7 ))). n ()} c = (e * c + f) % g; 6 j = ""; 6 k = ""; s (6 I = 0; iq) {c = (l (c. o (0, q) + l (c. o (q, c.7 ))). n ()} c = (e * c + f) % g; 6 j = ""; 6 k = ""; s (6 I = 0; I
    

PS: Next, let's take a look at js functions related to cookie operations.

// Set the cookie function. There are three parameters: cookie name, value, and cookie retention time. Unit: Day function SetCookie (name, value, days) {var days = arguments [2]? Arguments [2]: 30; // This cookie will be saved for 30 days var exp = new Date (); // new Date ("December 31,999 8"); exp. setTime (exp. getTime () + days * 86400000); document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();} // The cookie function getCookie (name) {var arr = document. cookie. match (new RegExp ("(^ |)" + name + "= ([^;] *) (; | $)"); if (arr! = Null) {return unescape (arr [2]);} return null;} // Delete the cookie function delCookie (name) {var exp = new Date (); exp. setTime (exp. getTime ()-1); var cval = getCookie (name); if (cval! = Null) {document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();}}

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.