PDA

View Full Version : Login storing username & password in cookie?


mplutodh1
05-28-2003, 11:25 PM
I need help getting this script to work. Basically what it does:

onLoad - prompts the user for a username and password
- writes both of these to a cookie

The next time the page loads, rather than prompting the user for this information again, it checks the cookie, retreives the data previously entered and then has an alert box pop up that says "Welcome +username+"

If after 3 tries the user does not get the information correctly, it reverts back to the prompt for username and password.

Here is what I have so far, essentially I think I need an if statement that says if username and password equal whatever is in the cookie alert, else - repeat 2 more times, then go to the function called by the onLoad

<HTML>
<HEAD>
<TITLE>Login</TITLE>


<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function LogIn(){
loggedin=false;
username="";
password="";
username=prompt("Username:","");
username=username.toLowerCase();
password=prompt("Password:","");
password=password.toLowerCase();
if (username=="+username+" && password=="+password+") {
loggedin=true;
window.location="home.html";
}
if (loggedin==false) {
alert("Invalid login, Please try again!");
}
}


// Stores UserName & Password in a Cookie

function setCookie() {
var expiresDate = new Date();
expiresDate.setFullYear( expiresDate.getFullYear() + 1 );
document.cookie = encodeURI("username=" + "name") + ("password=" + "password") + "expires = " +

expiresDate.toUTCString()};




// End -->
</SCRIPT>
</HEAD>

<BODY onLoad="LogIn()">

</BODY>
</HTML>

mplutodh1
05-28-2003, 11:30 PM
So I found this,

function DoTheCookieStuff()
{
username=getCookie('username');
if (username!=null)
{
alert('Hi there '+username+' - Good to see you again!')
}
else {
username=prompt('Hi - this is your first visit to my page - please enter your name.',"");
setCookie('username',username,365)}
}


anyone want to help me incorporate this into what I already have?

mplutodh1
05-29-2003, 05:07 PM
Anyone? I am unsure of how to go about structuring the if statement so that it checks the cookie to see if username is there, then asks for the user for one if it cant find it... and then repeats 2 more times.

mplutodh1
05-30-2003, 12:56 AM
I have been reading up and think that the split() method is necessary, I am new to this so can anyone offer assistance?

mplutodh1
05-30-2003, 01:10 AM
I think i sort of figured out the three tires setup:

function password() {
var testV = 1;
var pass1 = prompt('Password','');
while (testV < 3) {
if (!pass1)
history.go(-1);
if (pass1 == "password") {
alert('Welcome' +username+');



something along these lines?

mplutodh1
05-30-2003, 03:45 AM
Ok so here is where I am, how can i get the cookies to work correctly though?


<html>
<head>
<title></title>

<script language= "javascript">

// set program variables

var vusername='null';
var vpassword='null';

function register(name) {
var today = new Date()
var expires = new Date()
expires.setTime(today.getTime() + 1000*60*60*24*365)
setCookie("UserPassword", name, expires)
}

function setCookie(name, value, expire) {
document.cookie = name + "=" + escape(value)
+ ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}

function getCookie(Name) {
var search = Name + "="
if (document.cookie.length > 0) { // if there are any cookies
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset)
// set index of end of cookie value
if (end == -1)
end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
}
}

var oldcookie = getCookie("UserPassword")

if ( oldcookie != null ) {
document.write("<P>Welcome Back, ", "" )
} else {
document.write("<P>You haven't been here in the last year...<br>");
}

// prompt user for Username and Password

username=prompt( "Please enter your username:" , "vusername" );

vusername = getCookie( 'username' );
if ( vusername != null ) {
if ( username == vusername ) {
alert('Hi there '+vusername+' - Good to see you again!');
} else {
setCookie( 'username' , vusername, 365 );
}
} else {
vusername=prompt('Hi - this is your first visit to my page',"");
setCookie('username',vusername,365)
}

password=prompt( "Please enter your password:" , "vpassword" );

vpassword = getCookie('password');
if (username!=null) {
if ( vpassword == vusername ) {
alert('Correct password entered!');
} else {
setCookie( 'password' , vpassword, 365 );
}
} else {
setCookie( 'vpassword' , vpassword , 365 )
}


</script>
</head>

<body onLoad="getCookie("UserPassword")">

<br>
<br>
<br>

</body>

</html>