Gossamer Forum
Home : Products : DBMan : Customization :

cookies....

Quote Reply
cookies....
Hi all.. Smile

I've been working with cookies for the past few weeks now.. and I HATE em!!! But I need one for my db Frown

I'm wondering if anyone has a cookie working with DBman that saves the 'Userid' - 'Password' and 'Uid' of a user AFTER they have logged in?

I have seen the other cookie mod.. and this only serves half the purpose..


ANY help would be appreciated


------------------
-----------
millsie :)

A smile a day...
keeps the viri' away.
Quote Reply
Re: cookies.... In reply to
Hi millsie,
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 == "" ) &#0124; &#0124; ( 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
Quote Reply
Re: cookies.... In reply to
Hi Fred Smile

Many thanks.. however this cookie mod only achieves half the goal.. I need the 'UID' to be saved as well.. so I guess the cookie needs to be saved after the log-in process as taken place.. (after the uid has been assigned)..

I want the cookie to basically allow those that have logged in.. to be able to link straight back to thier homepage without haveing to log in a second time.. (unless the cookie expires).

I'll keep trying to work it out Smile

------------------
-----------
millsie :)

A smile a day...
keeps the viri' away.
Quote Reply
Re: cookies.... In reply to
Hi millsie

The only thing I can think of is to set another cookie after login for the uid.
I'm sure that could be done.

The trouble is that the session will eventually expire per the setting of $auth_time.

Making $auth_time really long seems problematic.

What is it you're trying to accomplish?

Fred
Quote Reply
Re: cookies.... In reply to
The reason I want a cookie of this type is..

1. So members can avoid the login screen when they leave the site and come back.. (say if they follow a link off the site.. then want to come back in an hour.. they have to log in every time.. which I don't want them to do.. it gets annoying if you have to do this a couple of times)

2. By getting a cookie working.. I can then work a post board into my site.. allowing for members names to be links to thier profiles etc..

Once I have this.. my membership db will be complete Smile

------------------
-----------
millsie :)

A smile a day...
keeps the viri' away.
Quote Reply
Re: cookies.... In reply to
Hey millsie,

If you want someone to log in one time, and forever after be able to return by link (or bookmark) bypassing the log in, I don't know how to approach that.

If you want your user to be able to click a link for a visit, and then return (bypassing the log in process), how about opening the link in a new window?

They could close the window when they're done, and "return" to the db.

html_record might look like this

[code}
<A HREF="" onClick=window.open("$rec{'url'}")>$rec{'url'}</A>[/code]
I haven't tried, but this should work "cross browser".

Fred

[This message has been edited by Fred (edited November 22, 1999).]
Quote Reply
Re: cookies.... In reply to
Hi Fred Smile

an easier way to open a new window is..

<A HREF="$rec{'url'}" target=new>$rec{'url'}</A>

Your way is used if you want to define what window attributes work in the new window.. ie.. scrollbars.. toolbars.. location bar etc..

Anyways.. you help has been appreciated.. many thanks Smile


------------------
-----------
millsie :)

A smile a day...
keeps the viri' away.
Quote Reply
Re: cookies.... In reply to
Millsie-

Did you ever get the session cookie to work? Has anybody else implemented this? This is high on my priority list, so if no one else has a solution, I'll probably take a crack at it real soon now...

------------------
The Immuatable Order of Modding
-=-=-=-=-=-=-=-
1. Read the FAQ, 2. Search the board, 2a. Search the board again, 3. ask the question, 4. back-up, 5. experiment, 6. rephrase question (or better yet, post solution to original question)
Quote Reply
Re: cookies.... In reply to
hey Smile

I'm afraid I'm still getting errors with the session cookie Frown

If you have a go at it.. please let me know if you get it to work... I'd surely appreciate a some help with it Smile


------------------
-----------
millsie :)

A smile a day...
keeps the viri' away.
Quote Reply
Re: cookies.... In reply to
If you have the above javascript working.....

From the javascript above
I added 1 line to function GetCookies()
Code:

// 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;
#### I added the following line #####
document.location.href='$db_script_url?userid='+GetCookie( "username" )+'&pw='+GetCookie( "password" )+'&login=1&cookie=1';}
}
}

If Remember password is "checked" on last log in,
this will auto login and pass cookie=1 to db.cgi
next time db.cgi is called.

Now, for the other half.

After login, DBMan will send us to the Main Menu .
so we change in html.pl
Code:
sub html_home {
# --------------------------------------------------------
# The database manager home page.
##### add....

if($in{'cookie'} =="1"){
print "Location: $db_script_link_url&Userid=$db_userid&view_records=1&ww=on\n\n";
}
else{
&html_print_headers;
print qq|
<html>
all the rest of the sub........

} #### close else
} #### close sub html_home

This will take user to view all their records
or by using &Userid=$db_userid you can send them
to whatever your subs desire.
I used ww=on so Dan will not see Danny's records.
This works for me. And it's a hack but it's a start.
Hope this helps?

Dave T
Quote Reply
Re: cookies.... In reply to
Many thanks DaveT Smile

This is pretty close.. but still doesn't save the 'uid'.

The main reason I want this to save the 'uid' is because I am trying to intergrate a message board with with my membership db.. and I only want those that are members to be able to use the message.. and unless I can get the cookies to save the uid.. the intergration is almost impossible.

By doing this.. I know I can cut down on all the spamming posts that I receive.

Many thanks.. you have brought me a step closer to what I need and given me a few new options to explore Smile




------------------
-----------
millsie :)

A smile a day...
keeps the viri' away.