Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl - Cookies & Variables

How to set a variable as a cookie?

         

NeueZiel

6:47 am on May 30, 2003 (gmt 0)

10+ Year Member


I have the following perl script (for a login-type page)
-----
#!/usr/local/bin/perl -w
print "Set-Cookie: $login[0]~$login[1]";
print "\n";
print "Content-type: text/html\n\n";
%formdata = getFormData();
$formdata{user} = lc($formdata{user});
$formdata{pass} = lc($formdata{pass});

open(DAT, "$formdata{user}.pl"[smilestopper]);
$rawdata=<DAT>;
close(DAT);

@login = split(/~/, $rawdata);

if($formdata{task} eq ""[smilestopper])
{
print "<html><head><title>Please Log In:</title></head><body bgcolor=#000000 text=#ffffff><form method=post action=test_Copy_1.pl?task=submit>";
print "User: <input type=text name=user><br>Pass: <input type=password name=pass>";
print "<input type=submit value=Submit></form>";
-----
(That's just an excerpt of it, by the way)

The subroutine that is being linked to on Line 5
%formdata = getFormData();
Takes the two input fields, named user and pass, and sticks them into two scalars (I'm skipping the hash step, that oughta be obvious), $formdata{user} and $formdata{pass}.

Now, as you can see above, $formdata{user} is used to open up a data file, of which it's contents are two words seperated by a ~. Those words are the username and password, respectively. These two words (split at the ~) get stuck into the array @login, so that's what those are. They become the scalars $login[0] and $login[1], if that needs to be said. :p

Now, my question is, is there a way to take these values ($login[0], $login[1]) and set them into the cookie which I'm trying to do in Line 2? That as it is just sets blanks (aka, nothing).

Any help would be appreciated, and that was probably confusing, so if I need to clarify anything, let me know.

Thank you!

andreasfriedrich

8:47 am on May 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may use either the Lincoln Steinīs CGI module or set the cookie in your http header directly:

[wp.netscape.com ] and [search.cpan.org ]

The syntax to set a cookie are as follows:

Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure

Thus you need to set a name=value pair which you did not do in your code.

Andreas

NeueZiel

6:47 pm on May 30, 2003 (gmt 0)

10+ Year Member


$login[0]~$login[1] is the NAME=VALUE pair. I'd just replaced the = with a ~ for easier splitting of the pair later. (I can't seem to get = to split... ~ always does though).