admin 管理员组文章数量: 1087817
js实现7天免登录
用js通过浏览器cookie实现7天记住密码
但实际环境不可用此方法,别人一查cookie就可以直接看到账号密码了
html代码:
<div><label for="username">用户名</label><input type="text" name="username" id="username"><br><label for="psw">密码</label><input type="text" name="psw" id="psw"><br><label><input type="checkbox" id="check">7天免登录</label><br><button type="button" id="btn">登录</button>
</div>
js代码:
<script>// 封装js// 参数为cookie名,对应值,以及有效日期function setCookie(cookName, cookieVal, expires) {let d = new Date();d.setDate(d.getDate() + expires)document.cookie = cookName + "=" + cookieVal + ";path=/;expires=" + d.toUTCString();}let oName = document.getElementById("username");let oPsw = document.getElementById("psw");let oCheck = document.getElementById("check");let oBtn = document.getElementById("btn");getCookie("user");function getCookie(cookName) {let cookie = document.cookie;console.log(cookie);// 这里要注意split是分号加空格(; )不是单独一个分号let arr = cookie.split("; ");console.log(arr);for (let i = 0; i < arr.length; i++){console.log(arr[i]);// let n = arr[i].split("=");let newArr = arr[i].split("=");console.log(newArr);if(newArr[0] == cookName){console.log(newArr[1]);let obj = JSON.parse(newArr[1]);console.log(obj);oName.value = obj.username;oPsw.value = obj.password;}}}oBtn.onclick = function () {if(oCheck.checked){console.log("选中");let obj = {};obj.username = oName.value;obj.password = oPsw.value;console.log(obj);let str = JSON.stringify(obj)setCookie("user", str, 7);}}</script>
本文标签: js实现7天免登录
版权声明:本文标题:js实现7天免登录 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1700275690a375681.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论