刚开始做这个功能的时候一头雾水,本来打算直接用cookie实现,但发现在控制台用document.cookie,就会完全暴露出用户名和密码,于是在网上找了各种办法,但没有找到一个很好的解决方案,最后想到在服务端设置cookie,并且经过加密来实现。
因为公司后端是php,所以以下用php为例。
前端部分:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>记住密码和自动登录</title>
</head>
<body>
<div>账号:<input type="text" id="account"></div><br>
<div>密码:<input type="password" id="password"></div><br>
<div>
<label for="remPassword"><input type="checkbox" id="remPassword">记住密码</label>
<label for="autoLogin"><input type="checkbox" id="autoLogin">自动登录</label>
</div><br>
<div><button type="button">登录</button></div>
<script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>
<!--
引入jquery.cookie是为了演示方便,可以通过后端语言读取cookie,并设置html
-->
<script src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script>
/