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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cookies

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
NL
I opened a thread in the javascript forum about cookies (I never worked with this!).

Luciddream responded that I should ask this here in the PPerl forum which I was actually about to do as I found somewhere that you can set cookies directly from Perl:

This is what it should look like according to this source:

Content-type: text/html
Set-Cookie: foo=bar; path=/; expires=Mon, 31-Jan-2001 00:00:00 GMT
I tried it but it returns errors. I guess it is not as simple as that.


What does "path=/" stand for?

And does anybody know what I should change to get this to work?

Ron
 
i haven't looked around at any of the modules, but, i think the CGI moduke can set cookies...

i usually use a library called cookie-lib, i dont remember where i found it, but, if i find it again, i'll post a link.

check cpan for CGI.pm for the CGI module. adam@aauser.com
 
btw. It also explained how to read the cookie:

@nvpairs=split(/; /, $ENV{'HTTP_COOKIE'});

foreach $pair (@nvpairs) {
($name, $value) = split(/=/, $pair);
$cookie{$name} = $value;
}


provided that the platform used supports the environment variable $ENV{'HTTP_COOKIE'}

But first I need to find out how to set the cookie correctly

Ron
 
Luciddream you were just ahead of me!

I'm afraid I have no idea how to start or deal with:

cpan for CGI.pm for the CGI module.

Ron
 
most of the things you want to do could probably be easily done with the CGI module. it provides easy OO (object oriented) methods for parsing forms, writing html and all kinds of neat other stuff.

i haven't ever really used it, but i've heard wonderful things about it. adam@aauser.com
 
Hey safra,
I use CGI.pm all the time. It does allow easy setting and retrieving of cookies..

A very simple example of setting/retrieving a cookie.....
Code:
#!/usr/local/bin/perl
use CGI;
$query = new CGI;
$thisCGI = $query->url();

# try to get a cookie named 'cookThis'
$cookThis = $query->cookie(-name=>'cookThis');

# IF the cookie named 'cookThis' alread exists
# set it value to null.
# ELSE set it's value to 'Some Cookie value here'.
# You should be able to watch the contents of the cookie file
# change back and forth with each run of this script.

if ($cookThis) 
    { 
    $cookie = $query->cookie(-name=>"cookThis",
        -value=>'',
        -expires=>"+1h");
    }
else
    {
    $cookie = $query->cookie(-name=>"cookThis",
        -value=>"Some Cookie value here",
        -expires=>"+1h");
    }

print $query->header(-cookie=>$cookie);
print &quot;<BR>CookThis is $cookThis<BR>&quot;; 
print $query->start_html(-title=>&quot;cook this&quot;);
print $query ->start_multipart_form('POST',&quot;$thisCGI&quot;);
print '<BR>',$query->submit('doWhat','cook');
print $query->end_form;
print $query->end_html;
[\code]

If you are going to use CGI.pm, you will need to let it build your HTML header stuff.

There are lots of other parameters you can use.  For a full explanation of the various parameters, see 'perldoc CGI.pm'.  If you need help understanding the Object syntax, I can explain that a little, also.

'hope this helps.  
 
 
 keep the rudder amid ship and beware the odd typo
 
Luciddream I downloaded the files. That is a lot of code! I will try to figure out how it works, thanks a lot!

goBoating, I tried what you posted:

first the the script opens a page:

&quot;CookThis is&quot;

submit button.

When pressed it redirects to the page but nothing changes and I can't see a new cookie in the temporarily Internet files.

Should I change something?
Where can I find 'perldoc CGI.pm'?

Ron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top