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

Cookies: writing and reading with CGI.PM

Status
Not open for further replies.

airmoti

MIS
Jan 28, 2004
6
US
I can set and read cookies with Javascript and I can read Javascript set cookies with CGI.pm.
However, I cannot set the cookies with CGI.pm.
Neither the cpan docs nor the Lincoln Stein book seem to mention that you need to do anything else to get this to work.
I do not have access to CGI::SESSION so that is not an option.

What are we missing here? One thing that seems odd is the: $query->header statement is printing its output to the browser. Am I missing a setting somewhere?

$query = new CGI;

$c = $query->cookie(-name=>'testCookie',
-value=>'2');

print $query->header(-cookie=> $c);

$testCookie = $query->cookie('testCookie');

print &quot;testCookie = $testCookie <br>&quot;;


 
Everything looks correct by the looks of it. Strange that it doesn't work. I will investigate further.

To set cookie:
use CGI;
$query = new CGI;

$cookie = $query->cookie(-name=>'mycookie',
-value=>'xyzzy',
-expires=>'+1h');
print $query->header(-cookie=>$cookie);

To retrieve data:
use CGI;
$query = new CGI;
$answer = $query->cookie('mycookie');


Sean.
psa.gif
 
I just tested this script and it worked:

copy and paste it and test it on your box:
Code:
#!/usr/bin/perl
                                                                                
use CGI;
$query = new CGI;
                                                                                
$cookie = $query->cookie(-name=>'mycookie',
             -value=>'xyzzy',
             -expires=>'+1h');
print $query->header(-cookie=>$cookie);
                                                                                
$query2 = new CGI;
$answer = $query2->cookie('mycookie');
                                                                                
print &quot;content-type:html/text\n\n&quot;;
                                                                                
print &quot;Answer= $answer&quot;;
[/color]
Tell me if it worked or not, and we will take it from there.


Sean.
psa.gif
 
Sean,

Here is what is displayed in the browser:

content-type:html/text Answer=
 
I think the cookie is being set, but you won't be able to retrieve it in the same script you set it (and there's really no reason to). Try setting the cookie with one script and retreiving it with another. sean4e's code works, but I'm betting you just copied and pasted it into one file, not two as was intended.

 
philote,
You are right. The code works if I change $query2 to just $query so I'm sure it would work in two different scripts.

This is strange. I am now able to get the cookie set if it is the very first thing I do in my CGI script.
But if I set the cookie well into the HTML being printed, (a perl subroutine) it does not seem to be set to the http header?

Also, adding the line: print &quot;content-type:html/text\n\n&quot;;
causes the browser to ask if I want to download the file to my hard drive.
 
I may be wrong about not being able to set and retreive a cookie in the same script but like I said, it's pointless to do so.

The cookie is transferred to the client in the header, so you'll need to set the cookie before the header is printed. If you have problems doing this, then instead of printing your html, append it to a variable(for example $html). Then at the end of your script print the header then print $html. However you should be able to reorganize your script so you don't have to do this.

 
Hi im new to perl and i followed you thread up to a point, i also took your script and split it so the first cgi created the cookie and the second half was to read it however it wanted to down load it to my computer if i select open file then it gives me the correct answer but in a txt file can you explain what im doing wron and show me some code to do it correctly.


Mad axe

#!/Perl/bin/perl
use CGI;
$query = new CGI;
$cookie = $query->cookie(-name=>'mycookie',
-value=>'xyzzy',
-expires=>'+1h');
print $query->header(-cookie=>$cookie);





#!/Perl/bin/perl

use CGI;
$query2 = new CGI;
$answer = $query2->cookie('mycookie');

print "content-type:html/text\n\n";
print "Answer= $answer";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top