/    Sign up×
Community /Pin to ProfileBookmark

(noobie)user log in codes need help

hi i’m a noobie to javascritps. i’m trying to make a user log in page, which will use cookies to store username and password. when a old user is re-visiting the site, they will see a welcome message,and it just asks them to enter the password, but there was a error, the welcome message pops up 3 times. i only want it to pop up once.

also when a new member registers, then can enter the site without entering a username and password, i dont want that to happen.

Below is the code for the page. PLease can some tell me how to fix it up and is theres other problems with it that i havent found out.

<html>
<head>
<title>Personal Preferences</title>
<script language=”JavaScript”>
<!– HIDE FROM INCOMPATIBLE BROWSERS

var inputName

var inputPassword
var usertype

function setCookie() {
var expiresDate = new Date();
expiresDate.setMonth(expiresDate.getMonth() + 1);
document.cookie = encodeURI(“user_name=” + document.user_prefs.user_name.value) +
“; expires=” + expiresDate.toUTCString();
document.cookie = encodeURI(“password=” + document.user_prefs.password.value) +
“; expires=” + expiresDate.toUTCString();
alert(“Your name and password have been saved in a cookie.”);
location.href = “ProductRegistration.html”;
}//end setCookie

function getUserPrefs() {
var userPrefs = decodeURI(document.cookie);
if (userPrefs.length != 0)
{
usertype = “old”
var prefsArray = userPrefs.split(“; “);
var curCookie;
for (var count = 0; count < prefsArray.length; ++count)
{
curCookie = prefsArray[count];
if (curCookie.substring(0,curCookie.indexOf(“=”)) == “user_name”)
{
inputName = curCookie.substring(curCookie.indexOf(“=”) + 1, curCookie.length);
}//end if
if (curCookie.substring(0,curCookie.indexOf(“=”)) == “password”)
{
inputPassword = curCookie.substring(curCookie.indexOf(“=”) + 1, curCookie.length);
}//end if
alert(“Welcome ” + inputName + “. Please type your password.”);
document.user_prefs.user_name.value = inputName;
document.user_prefs.password.focus()
}//end for
}//end if
else
{
usertype = “new”
alert(“You are a new user. Please register your details.”);
document.user_prefs.user_name.focus()
}//end else
}//end getUserPrefs

function checkLogin()
{
if (usertype == “new”)
{
setCookie();
alert(“Your Registration has been successful”);
location.href = “ProductRegistration.html”;
}//end if

else if (inputName != document.user_prefs.user_name.value || inputPassword != document.user_prefs.password.value)
{
var register = confirm(“Invalid login. Do you wish to re-register?”);
if (register == true)
{
usertype = “new”
document.user_prefs.user_name.value = “”;
document.user_prefs.password.value = “”;
document.user_prefs.user_name.focus();
}
else
{
alert(“Re-enter your password”);
document.user_prefs.password.value = “”;
document.user_prefs.password.focus();
}//end if
}//end else
else
{
alert(“You have successfully logged in.”);
location.href = “ProductRegistration.html”;
}
}//end checkLogin

//–>
</script>
</head>

<center>
<body onLoad=”getUserPrefs();” id=”docBody”>
<form name=”user_prefs”>
<table width=225 border=1 cellpadding=3>
<tr><td colspan=2><center><font size=”+2″><b>Members-Only Area!</b></font></center></td></tr>
<tr><td>Username:</td><td><input type=text name=user_name></td></tr>
<tr><td>Password:</td><td><input type=text name=password></td></tr>
<tr><td colspan=2 align=center><input type=button value=”Login!” onClick=”checkLogin()”></td></tr>
</table>
</form>
</center>
</body>
</html>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Brandoe85Nov 20.2004 — Is there a reason for this? Storing a username and password in cookies is not the way to go....
Copy linkTweet thisAlerts:
@JuuitchanNov 20.2004 — [B]NO NO NO NO NO[/B]

This is [B]deceptive[/B]. They will think they are actually logging into your site even though they aren't. So if they access your site through a library computer or through a friend's computer, they will want to know what the heck is going on when the computer is asking them to re-register. They will complain to you. Also, they will have a false sense of security that others can't "log in" as them, when it is easy (just go to a different computer and use their name and a phony password, or else clear cookies). I repeat, [B]this script is deceptive.[/B] It is immoral to use it.
×

Success!

Help @hisa spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...