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

CGI and cookies

Status
Not open for further replies.

c4n

Programmer
Mar 12, 2002
110
0
0
SI
Hi,

A question about CGi and cookies:

How do I set a cookies with CGI (Let's say that it has value = "mycookies" and expires after 6 months) and then if the user returns read from it (if found do this, else do that).

Thanks in advance!
 
You're best bet for anything CGI is the CGI object. You create one in perl with the following code:

Code:
use CGI;
$cgi = new CGI;

Then you can create a cookie with this code:

Code:
# create a cookie
$cookie = $cgi->cookie(-name=>'sessionID',
                       -value=>'xyzzy',
                       -expires=>'+1h',
                       -path=>'/cgi-bin/database',
                       -domain=>'.capricorn.org',
                       -secure=>1);
# tell the browser to set the cookie
print $cgi->header(-cookie=>$cookie);

To get the value back, create a CGI object as shown above and use the following:

Code:
$sessionID = $cgi->cookie('sessionID');

For more information on the CGI object check out
Hope this helps!
Chris
 
Can't get the script to create a cookie :( Here's what I tried:
------------------------------------


#!/usr/bin/perl -w
use CGI;
$cgi = new CGI;

$cookie = $cgi->cookie(-name=>'test',
-value=>'testvalue',
-expires=>'+1h');
print $cgi->header(-cookie=>$cookie);
&message;


sub message {
print "Content-type: text/html\n\n";
print &quot;<h1>COOKIE SET</h1>\n&quot;;
exit;
}

----------------------------------

The script just doesn't set the cookie. Any idea why?

Thanks
 
oh, withat tht &quot;-w&quot;, just &quot;#!/usr/bin/perl&quot;
 
Here's what you're script is outputting:

[tt]Set-Cookie: test=testvalue; path=/; expires=Sun, 24-Mar-2002 20:54:30 GMT
Date: Sun, 24 Mar 2002 19:54:30 GMT
Content-Type: text/html; charset=ISO-8859-1

Content-type: text/html

<h1>COOKIE SET</h1>[/tt]

What you're getting is two &quot;Content-type&quot; lines which could be you're problem. The header routine prints one of these lines so there is no need to print your own. Try removing that line.

[peace] Chris
 
Hi!

Sorry to bother you again, but I just can't get it to work :(

Is it possible that the &quot;set Cookie&quot; header works only with Netscape (I use IE)?

Here's the codes I used but don't work (any new ideas?):

---------------------------
**************
To set the cookie:
**************

#!/usr/bin/perl
use CGI;
$cgi = new CGI;

$cookie = $cgi->cookie(-name=>'test',
-value=>'testvalue',
-expires=>'+1h');
print $cgi->header(-cookie=>$cookie);
&message;


sub message {
print &quot;<h1>COOKIE SET</h1>\n&quot;;
exit;
}

------------------
***********
TO get the value back:
***********

#!/usr/bin/perl
use CGI;
$cgi = new CGI;

$cookievalue = $cgi->cookie('test');

&message;


sub message {
print &quot;<h1>COOKIE SET TO VALUE: $cookievalue</h1>\n&quot;;
exit;
}

---------------------

Thanks in advance!
 
The only problem I ran into when running your scripts is the fact that you do need to print a header when you retrieve the cookie. IE is capable of handling cookies (that's what I'm using) so that isn't your problem. Try putting the line:

Code:
print $cgi->header;

before your call to
Code:
message()
.

If that still does not work, then you'll need to look into the
Code:
-domain
parameter when you're creating the cookie.

[peace] Chris
 
I have had problems with cookies along the same type. The solution was that the domain parameter had to be specified. Depending on the type of server software begining used this can be a requirement to get the cookie to be retrievable. Try setting the domain parameter as mentioned above.
 
Technically, unless you specify a domain the cookie should only be retrievable by the page which set it. If you set the domain the just your domain name, without a page or directory, it should be retrievable by any page on your domain. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Thanks, will try this later and see what I get!
 
Hmmmm, seems like retrieving a cookie isn't the issue - the script doesn't even set one! I configured my brovser (IE5.5) to prompt when receiving a cookie and nothing happens when I execute the &quot;Cookie setup&quot; script.

 
Try this. To set the cookie
######################
#!/usr/bin/perl
use CGI;
$cgi = new CGI;

$cookie = $cgi->cookie(-name=>'test',
-value=>'testvalue',
-expires=>'+1h',
-domain=>'.yourdomain.com',
-path=>'/');
print $cgi->header(-cookie=>$cookie); #<-fordebugging
print $cgi->header(-cookie=>$cookie);
&message;


sub message {
print &quot;<h1>COOKIE SET</h1>\n&quot;;
exit;
}

################
Different script to get cookie
##############
#!/usr/bin/perl
use CGI;
$cgi = new CGI;

$cookie1 = $cgi->cookie(-name=>'test');
print $cgi->header();
print &quot;This is the cookie I picked up -- $cookie1 --&quot;;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top