Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with cookies

Status
Not open for further replies.

bench

Technical User
Jan 5, 2001
17
0
0
US
Can someone help me modify this "password example" so that it works. Currently it just calls "alert()" statements when it is suppose to save, get, and delete a cookie. I need to replace those "alert()" calls with the appropriate cookie call (GetCookie,
SetCookie, DeleteCookie
This program stores a password in the user's browser's cookie file, and then checks that file in the future to confirm the password. Display a 'password correct' or a 'password failed' message. I also want to have a button that deletes the cookie!!








<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 3.2//EN&quot;>
<HTML>
<HEAD>
<TITLE></TITLE>
<script language=&quot;JavaScript&quot;>

<!-- hide from non-JavaScript browsers
Code:
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (&quot;;&quot;, offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var arg = name + &quot;=&quot;;
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(&quot; &quot;, i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + &quot;=&quot; + escape (value) +
    ((expires == null) ? &quot;&quot; : (&quot;; expires=&quot; + expires.toGMTString())) +
    ((path == null) ? &quot;&quot; : (&quot;; path=&quot; + path)) +
    ((domain == null) ? &quot;&quot; : (&quot;; domain=&quot; + domain)) +
    ((secure == true) ? &quot;; secure&quot; : &quot;&quot;);
}

function DeleteCookie(name) {
  var exp = new Date();
  FixCookieDate (exp); // Correct for Mac bug
  exp.setTime (exp.getTime() - 1);  // This cookie is history
  var cval = GetCookie (name);
  if (cval != null)
    document.cookie = name + &quot;=&quot; + cval + &quot;; expires=&quot; + exp.toGMTString();
}


var expdate = new Date();
expdate.setTime(expdate.getTime()+(24*60*60*1000));
//expires in 24 hours - please use this!

// end hide -->

</script>
</HEAD>
<BODY>
Note: This example does not work. I need some help to make it work! <br>
All the pieces are here and in the &quot;cookie&quot; example. <br>
Display a 'password correct' or a 'password failed' message. <br>
I need to use the expiration date declared in the code for my cookie. <br>
I need to replace the &quot;alert&quot; functions with calls to cookie functions. <br>
<hr>
<br>
<form name='pwCheck'>

Enter a password to be stored in a cookie:
<input type='password' name='pwEnter' size=8><br>
<input type='button' value='Submit Password to Cookie' onClick=&quot;alert(document.pwCheck.pwEnter.value)&quot;><br>

<hr>

Enter a password to be checked:
<input type='password' name='pwConfirm' size=8><br>
<input type='button' onClick=&quot;alert(document.pwCheck.pwConfirm.value)&quot; value='Confirm Password from Cookie'><br>

<hr>

<input type='button' onClick=&quot;alert('Please delete the cookie!!')&quot; value='Delete Password Cookie'><br>


</form>

</BODY>
</HTML>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top