Javascript login cookies
inninn
Status: New User - Welcome
Joined: 21 Mar 2006
Posts: 1
Reply Quote
Hi,
Have got the following login page done in javascript, i need to know how to use cookies or perhaps some other way to prevent a user from directly accessing the page that loggin in takes you to...
Hope someone can help me......

<SCRIPT LANGUAGE="JavaScript">


<!-- Begin
function Login(){
var done=0;
var username=document.login.username.value;
username=username.toLowerCase();
var password=document.login.password.value;
password=password.toLowerCase();
if (username=="member1" && password=="password1") { window.location="Staff.htm"; done=1; }
if (username=="member2" && password=="password2") { window.location="Staff.htm"; done=1; }

if (done==0) { alert("Incorrect username or password, please try again"); }
}
// End -->
</SCRIPT>


and here's the form:

<form name=login>
<table width=281 border=1 cellpadding=3>
<tr><td colspan=2><center>
<font size="+2"><b>Staff-Only Area</b></font>
</center></td></tr>
<tr><td width="71">Username:</td><td width="200"><input type=text name=username></td></tr>
<tr><td>Password:</td><td><input type=password name=password></td></tr>
<tr><td colspan=2 align=center><input type=button value="Login!" onClick="Login()"></td></tr>
</table>
</form>
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4126
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
Sorry innin, we don't write people's scripts for them, that's a policy we try to follow since otherwise we'd be writing scripts for people all day long, for free.

However, using javascript isn't a very good idea for a login page since if you don't want people to be able to access it all you'd have to do is turn off javascript to get into it.

You should use php or some other server side language to handle this.

You can read more about javascript cookies on our site, that also has directions on how to implement that particular script. That's a very easy way to do cookies with javascript by the way.

The logic in either case is simple: check for login success, set cookie on success, and on the new page, check for the login cookie, if present, show page, if not present redirect or show error message.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours