I found this in the forum, and it seems to work pretty well.
Code:
##############################################################################
C O O K I E M O D
Written by: Eli Finkelman
finkelman@earthlink.net
Here's a MOD that will give the user the option to save their username
and password as a cookie on their hard drive. Next time they go to your
page, they won't have to enter all their login info, it will already be
filled in, and they just hit enter. I also included with this MOD a
little feature that will prompt the user if they entered data in only one
field. So if they only enter their User ID and then hit enter, a popup
screen will come up prompting that both fields must be filled in.
##############################################################################
Okay here we go. This Cookie MOD uses Javascript, so there is no need to
add any modules or addons to your script.
##############################################################################
1. Go to your cfg file (default.cfg) and add the following somewhere
in the middle.
##############################################################################
$print_get_cookies = "1";
##############################################################################
2. Add a new subroutine somewhere in your html.pl file
##############################################################################
sub cookie {
# --------------------------------------------------------
print qq~
<SCRIPT LANGUAGE=JAVASCRIPT>
// Extract the value from the cookie at the given offset.
function GetValue( Offset )
{
var End = document.cookie.indexOf (";", Offset);
if( End == -1 )
End = document.cookie.length;
// Return the portion of the cookie beginning with the offset
// and ending with the ";".
return unescape( document.cookie.substring( Offset, End) );
}
function GetCookie( Name )
{
var Len = Name.length;
// Look at each substring that's the same length as the cookie name
// for a match. If found, look up the value and return it.
var i = 0;
while( i < document.cookie.length )
{
var j = i + Len + 1;
if( document.cookie.substring( i, j) == (Name + "=") )
return GetValue( j );
i = document.cookie.indexOf( " ", i ) + 1;
if( i == 0)
break;
}
var a = "";
return a;
}
// Create or change a cookie given its name and value. The name and value
// are required, but the expiration date isn't. Note that if you don't specify
// an expiration date, the cookie only exists for the current session.
function SetCookie( Name, Value, Expire )
{
document.cookie = Name + "=" + escape( Value ) + ";expires=" + Expire;
}
// Write all the cookies for the form1 form.
function WriteCookies()
{
// var Expire = "Friday,25-Feb-2000 12:00:00 GMT";
var Expire = "$cookie_expiration_date";
with( document.form1 )
{
SetCookie( "username", userid.value, Expire );
SetCookie( "password", pw.value, Expire );
}
}
// Load the form with the values in the cookie
function GetCookies()
{
with( document.form1 )
{
userid.value = GetCookie( "username" );
pw.value = GetCookie( "password" );
auth_remember_login.value = GetCookie( "remember_login" );
if ( auth_remember_login.value == "on" ) {
auth_remember_login.checked = true; }
}
}
function FixCookieDate (date) {
var base = new Date(0);
var skew = base.getTime(); // dawn of (Unix) time - should be 0
if (skew > 0) // Except on the Mac - ahead of its time
date.setTime (date.getTime() - skew);
}
var expdate = new Date ();
FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 365)); // 365 days from now
function IsValid()
{
blnValid = true;
with( document.form1 )
{
if(( userid.value == "" ) | | ( pw.value == "" ))
{
window.alert( "You must enter both your user name and your password" );
blnValid = false;
}
var Username = userid.value;
var Password = pw.value;
if (auth_remember_login.checked) {
var Remember_login = "on";
}
}
if( blnValid )
if (Remember_login == "on") {
document.cookie = "username=" + Username + ";expires=" + expdate.toGMTString() + ";";
document.cookie = "password=" + Password + ";expires=" + expdate.toGMTString() + ";";
document.cookie = "remember_login=" + Remember_login + ";expires=" + expdate.toGMTString() + ";";
}
if (Remember_login != "on") {
var Expires = "-1";
document.cookie = "username=" + Username + ";expires=" + Expires + ";";
document.cookie = "password=" + Password + ";expires=" + Expires + ";";
document.cookie = "remember_login=" + Remember_login + ";expires=" + Expires + ";";
}
return blnValid;
}
</SCRIPT>~;
}
##############################################################################
3. Go to your sub html_login_form routine. We're going to have to do a
little editing. At the top of the routine Change
##############################################################################
<body bgcolor="#DDDDDD" text="#000000">
##############################################################################
To
##############################################################################
<body bgcolor="#DDDDDD" text="#000000"|;
if ($print_get_cookies) { print qq~ OnLoad="GetCookies()"~; }
print qq|>|;
&cookie;
print qq|
##############################################################################
Change
##############################################################################
<form action="$db_script_url" method="post">
##############################################################################
To
##############################################################################
<form action="$db_script_url" method="POST" name="form1" OnSubmit="return IsValid()">
##############################################################################
Under
##############################################################################
<td><input type="PASSWORD" name="pw"></td></tr>
##############################################################################
Add
##############################################################################
<tr><td colspan=2><b>Remember username and password for future logins?</b>
<INPUT type="checkbox" name="auth_remember_login">
</td></tr>
##############################################################################
Well that's it, it should work just fine. Let me know if this doesn't
work out for you, I'll gladly help.
##############################################################################
Eli Finkelman
finkelman@earthlink.net