目前,您进入页面后,将允许一个人注册一个帐户。
是的,我知道这不是应该怎么做。我只是在摆弄饼干。有没有办法做到这一点,以便可以有多个帐户而不是一个帐户。
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function registerCookie() {
var user = prompt("Please enter your desired username:","");
var pass = prompt("Please enter your desired password:","");
if (user != "" && user != null && pass != "" && pass != null) {
setCookie("username", user, 30);
setCookie("password", pass, 30);
}
}
function loginCookie() {
var user=getCookie("username");
var pass=getCookie("password");
var check=prompt("Username");
var check2=prompt("password");
if (check == user && check2 == pass) {
alert("Welcome back, " + user);
} else {
alert("False information");
}
}
function start() {
var response = prompt("Would you like to register?");
if (response == "yes" || response == "Yes") {
registerCookie();
} else if (response == "no" || response == "No") {
loginCookie();
} else {
alert("We did not understand your input");
}
}
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body onload="start()">
</body>
</html>
最佳答案
我认为我们可以在这里使用一些技巧:使用用户名作为cookie的名称
function registerCookie() {
var user = prompt("Please enter your desired username:","");
var pass = prompt("Please enter your desired password:","");
if (user != "" && user != null && pass != "" && pass != null) {
setCookie("username_"+user, user, 30);
setCookie("password_"+user, pass, 30);
}
}
function loginCookie() {
var checkUser=prompt("Username");
var checkPass=prompt("password");
var user=getCookie("username_"+checkUser);
//alert(user)
var pass=getCookie("password_"+checkUser);
//alert(pass)
if (checkUser == user && checkPass == pass) {
alert("Welcome back, " + user);
} else {
alert("False information");
}
}
关于javascript - 如何允许使用Cookie注册多个帐户?来源提供,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53077397/