简单操作 Cookie 注册登录

给大家推荐个靠谱的公众号程序员探索之路,大家一起加油https://img-blog.csdnimg.cn/20181129224604602.png

注意  :现在 一般来说浏览器都会禁止访问Cookie 建议在IE浏览器上操作

<!DOCTYPE html>
<html>
  <head>
    <title>Main.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
	<script type="text/javascript">
		//将用户名 密码  有效期存储到cookie中
		function setCookie(name,value,days){
			var date = new Date();
			date.setDate(date.getDate() + days);
			if(getCookie(name).length < 0){
				document.cookie=name+"="+escape(value)+";"+((days==null)?"":"dates="+date.toGMTString());
				return true;
			}else{
				return false;
			}
			
		}
		//cookie中 存储是按照键值对 存储的  
		//根据name获取对应的值
		function getCookie(name){
			if(document.cookie.length > 0){
				var start = document.cookie.indexOf(name + "=");
				var end = -1;
				if(start != -1){
					start = start + name.length + 1;
					end = document.cookie.indexOf(";" , start);
					if(end == -1){
						end = document.cookie.length;
					}
					return document.cookie.substring(start , end);
				}
			}
			
		}
		//注册功能
		function register(){
			var name = document.getElementById("name1").value;
			var password = document.getElementById("password").value;
			if(setCookie(name, password, 365)){
				document.getElementById("name1").value="";
				document.getElementById("password").value="";
				alert("注册成功");
			}else{
				alert("注册失败!" + "该用户已存在!");
			}
		}
		//登录功能
		function login(){
			var name = document.getElementById("name1").value;
			var password = document.getElementById("password").value;
			if(getCookie(name) == password){
				alert("登录成功");
				document.getElementById("name1").value="";
				document.getElementById("password").value="";
			}else{
				alert("登录失败" + "用户名或者密码错误");
			}
		}
		function show(){
			if(document.cookie.length <= 0){
				document.getElementById("div").innerHTML="没有内容";
			}else{
				document.getElementById("div").innerHTML=document.cookie;
			}
		}
	</script>
  </head>
  
  <body>
  	用户名:<input id="name1" type="text"/><br>
  	    密码:<input id="password" type="password"/><br>
    <input type="button" value="登录" onclick="login()"/>
    <input type="button" value="注册" onclick="register()"/>
    <input type="button" value="显示" onclick="show()">
    <div id="div"></div>
  </body>
</html>

 

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Node操作MongoDB数据库进行注册和登录功能,你需要遵循以下步骤: 1. 安装依赖:使用npm或yarn安装Node.js和MongoDB驱动程序。 2. 启动MongoDB:确保MongoDB服务器正在运行,并且你有访问权限。 3. 连接到数据库:使用MongoDB驱动程序连接到数据库。这可以通过设置MongoClient实例来实现。 4. 创建用户模型:定义用户注册和登录时需要的用户模型。这可以是一个简单的JavaScript对象,包含用户名、密码等字段。 5. 注册功能:当用户输入用户名和密码时,将其保存到数据库中。你可以使用MongoDB的insertOne或insertMany方法将用户数据插入到集合中。 6. 登录功能:当用户输入用户名和密码时,从数据库中查找具有相同用户名的记录。如果找到匹配的记录,则用户可以登录。 7. 用户验证和会话管理:可以使用jsonwebtoken或类似的库生成并验证带有权限信息的令牌。你可以将令牌存储在用户的浏览器Cookie中,以便在后续请求中进行身份验证。 8. 路由设置:使用Express或类似的框架设置路由,以便在特定的URL上处理注册和登录请求。 9. 错误处理:在处理请求过程中,验证输入的用户数据并处理任何可能的错误。例如,如果用户输入的用户名已经存在于数据库中,则返回适当的错误消息。 10. 安全性考虑:对于密码,应使用加密算法进行哈希处理,并在数据库中存储哈希值。确保使用安全的哈希算法,如bcrypt或scrypt。 这些是在Node中使用MongoDB进行注册和登录的基本步骤。你可以在此基础上构建更复杂的逻辑,如密码重置、邮箱验证等功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值