Cookie saves user name and account password

Source: Internet
Author: User
Tags guid identifier

Cookiesremember.js

/**
* 2015-8-6
* AUTHOR:DZQ
* Remember account number and password
*/
Get the last logged on user
function Getlastuser () {
var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";//guid identifier
var name = GetCookie (ID);//Take the value of the cookie
if (name! = null) {
document.getElementById (' user_name '). Value = name;
} else {
document.getElementById (' user_name '). Value = "";
}
Getpwdandchk ();//Check to see if there is a corresponding password exists cookie
}
Call this method when the user name loses focus to see if there is a corresponding password for the existence of a cookie
function Getpwdandchk () {
var name = $ ("#user_name"). Val ();
var password = getcookie (name);//Take the value of the cookie
if (password! = null) {
document.getElementById (' Checkboxlogin '). Checked = true;
document.getElementById (' User_password '). Value = password;
} else {
document.getElementById (' Checkboxlogin '). checked = false;
document.getElementById (' User_password '). Value = "";
}
}
Take the value of the cookie
function GetCookie (name) {
var arg = name + "=";
var alen = Arg.length;
var clen = document.cookie.length;
var i = 0;
while (I < Clen) {
var j = i + Alen;
/* Alert (j); */
if (Document.cookie.substring (i, j) = = arg)
Return Getcookieval (j);//Take the value of the cookie
i = Document.cookie.indexOf ("", I) + 1;
if (i = = 0)
Break
}
return null;
}
Take the value of the cookie
function Getcookieval (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr = =-1)
Endstr = Document.cookie.length;
Return unescape (document.cookie.substring (offset, endstr));
}
Save the user name of the last logged-on user
function Setlastuser (name) {
var id = "49bac005-7d5b-4231-8cea-16939beacd67";
var expdate = new Date ();
Current time plus two-week time
Expdate.settime (Expdate.gettime () + 14 * (24 * 60 * 60 * 1000));
Setcookie (ID, name, expdate);//write to Cookie
}
Write to Cookie
function Setcookie (name, value, expires) {
var argv = setcookie.arguments;
In this example, length = 3
var argc = SetCookie.arguments.length;
var expires = (argc > 2)? ARGV[2]: null;
var path = (argc > 3)? ARGV[3]: null;
var domain = (argc > 4)? ARGV[4]: null;
var secure = (argc > 5)? ARGV[5]: false;
Document.cookie = name + "=" + Escape (value) + ((expires = = null)? "" : ("; Expires= "+ expires.togmtstring ())) + ((path = = null)? "" : ("; Path= "+ path") + ((domain = = null)? "" : ("; domain= "+ domain") + ((secure = = True)? "; Secure ":");
}
If you do not select the Remember password and the current user is a cookie, you can also erase the cookie
function Resetcookie () {
Alert ("Remember password not checked");
var name = $ ("#user_name"). Val ();
var expdate = new Date ();
Setcookie (name, null, expdate);//write to Cookie
}

Cookiesremember.js----End


user-login.jsp

<script type=text/javascript>

$ (function () {
Getlastuser ();
});

Login
function Login () {
var name = $ ("#user_name"). Val ();
var password = $ ("#user_password"). Val ();
var newurl = "${url}";
if (name = = "" | | Password = = "") {
Window.wxc.xcConfirm (' User name and password cannot be null ', window.wxc.xcConfirm.typeEnum.info);
Return
}

var params = {
"Name": Name,
"Password": password
};
Writes the last user information to a cookie
Setlastuser (name);
if (!$ ("#checkboxLogin"). attr ("checked") ==false) {
Alert ("You have chosen to remember the password, we will remember your password:" +password);
var expdate = new Date ();
Expdate.settime (Expdate.gettime () + 14 * (24 * 60 * 60 * 1000));
Write a user name and password to a cookie
Setcookie (Name,password, expdate);
}
else {
Expire immediately if the remember password is not checked
Resetcookie ();
}

$.ajax ({
Type: "POST",
timeout:20000,
URL: ' <%=request.getcontextpath ()%>/user/passport/login ',
Data:params,
DataType: "JSON",
Success:function (data) {
if (Data.returncode = = 0) {
if (Newurl! = NULL && Newurl! = "") {
Window.location.href = "<%=request.getcontextpath ()%>" + "/" +NEWURL;
} else {
Window.location.href = "<%=request.getcontextpath ()%>/user/zonglan";
}
}else{
Window.wxc.xcConfirm (data.returnmsg, window.wxc.xcConfirm.typeEnum.error);
}
},
Error:function (XMLHttpRequest, Textstatus, Errorthrown) {
if (textstatus = = ' Timeout ') {
Window.wxc.xcConfirm (' Request timed out ', window.wxc.xcConfirm.typeEnum.error);
}else{
Window.wxc.xcConfirm (' connection exception ', window.wxc.xcConfirm.typeEnum.error);
}
},
Complete:function (XHR, ts) {

}
});
}

</script>

<input type= "text" placeholder= "Please enter phone number/mailbox" class= "Login_int" id= "user_name" onblur= "Getpwdandchk ()" >

<input type= "password" placeholder= "Please enter password" class= "Login_int" id= "User_password" >

<a href= "javascript:void (0);" onclick= "Login ();" > Login Now </a>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cookie saves user name and account password

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.