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!

Trouble in set/read/delete cookies using CGI.pm

Status
Not open for further replies.

sunw

Technical User
Sep 24, 2007
32
US
I wrote a small perl cgi code to test set/read/delete cookie as follows.

Code:
use strict;
use CGI::Pretty qw( :html3 );
use CGI::Cookie;

local $| = 1;

my $cookieName = 'ckname';
my $userInput = 'input';

my $q = new CGI::Pretty;

if($q->param('ok') == 1) {
  my $val = $q->param($userInput);
  my $ck = new CGI::Cookie(-name=>$cookieName,-value=>$val);
  print $q->header(-cookie=>$ck);
}

my %cookies = fetch CGI::Cookie;

[COLOR=red]
if(defined($cookies{$cookieName})) {
  my $ck = new CGI::Cookie(-name=>$cookieName,-value=>'');
  print $q->header(-cookie=>$ck);
  print "Cookie is found!!";
  exit;
}
[/color]

my $html = $q->start_html();
$html .= $q->start_multipart_form(-name=>'theForm');
$html .= $q->hidden(-name=>'ok',-value=>'1');
$html .= $q->div($q->label('Type something: '), $q->input({-name=>$userInput,-type=>'text'}));
$html .= $q->div($q->submit());
$html .= $q->end_form();
$html .= $q->end_html();
print $q->header;
print $html;

After submit, at the first time, the 'if' block in red does not get executed. Instead, I still get the form but with the html header (Content-Type: text/html;) printed out on my browser. At this point, if I hit 'F5' key to reload, then the 'if' block in red get executed. And what I see on my browser is like this:

Code:
Set-Cookie: ckname=; path=/ Date: Fri, 05 Oct 2007 20:29:45 GMT Content-Type: text/html; charset=ISO-8859-1 Cookie is found!!

And worse, the cookie never got deleted! I keep getting the same information shown above after repeating pressing F5 key.

What I expect is that after clicking 'Submit' button, I should see “Cookie is found!!” ONLY. Next, if I press F5 key, I'll get the form again, 'cuase the cookie should be deleted.

Could someone tell me what I did wrong in my code? Thanks.
 
A follow up question.

After the cookie is set by the code above, why can not I see the cookie in the cookie file on my computer. I am using a linux machine and I noticed there is a file called cookies.txt in ~/.mozilla/firefox/.../. I can not see anything changed in cookies.txt before/after submiting the form. Why so?
 
You have to do at least one refresh to read the cookie you just set.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you, Kevin.

I have modified my code and now it can find the cookie but still can not delete the cookie. Would you or someone else please tell me what I did wrong?

Code:
# The following code gets a user input and sets a cookie accordingly.
use strict;
use CGI::Pretty qw( :html3 );
use CGI::Cookie;

local $| = 1;
my $cookieName = 'ckname';
my $userInput = 'input';
my $q = new CGI::Pretty;

if($q->param('ok') == 1) {
  my $val = $q->param($userInput);
  my $ck = new CGI::Cookie(-name=>$cookieName,-value=>$val);
  print $q->header(-cookie=>$ck);
  print 'Please click '.$q->a({-href=>'./[COLOR=red]ck2.pl[/color]'}, 'here');
  exit 0;
}

my $html = $q->start_html();
$html .= $q->start_multipart_form(-name=>'theForm');
$html .= $q->hidden(-name=>'ok',-value=>'1');
$html .= $q->div($q->label('Type something: '), $q->input({-name=>$userInput,-type=>'text'}));
$html .= $q->div($q->submit());
$html .= $q->end_form();
$html .= $q->end_html();
print $q->header;
print $html;
exit 0;

Here is the implementation of ck2.pl:

Code:
# This code will detect if there is a cookie called 'ckname'. If yes, delete this cookie.
use strict;
use CGI::Pretty qw( :html3 );
use CGI::Cookie;

local $| = 1;
my $cookieName = 'ckname';
my $q = new CGI::Pretty;
my %cookies = fetch CGI::Cookie;

if(defined($cookies{$cookieName})) {
  my $ck = new CGI::Cookie(-name=>$cookieName,-value=>'');
  print $q->header(-cookie=>$ck);
  print "Cookie is found!!".$q->br();
  foreach my $x (keys(%cookies)) {
    print "\$x = $x".$q->br();
  }
  exit;
}[COLOR=blue]
else {
  print $q->header;
  print "Cookie '$cookieName' is NOT found!!";
  exit;
}[/color]

What I don't understand is after I pressing F5 to refresh ck2.pl, the code in blue is still NOT executed. In other words, it seems to me that the cookie 'ckname' never gets deleted.

So, could someone here tell me
1) how to delete a cookie?
2) how to tell if a cookie exists in my local machine? For instance, I am using firefox on linux. I know there is a file called 'cookies.txt' under $HOME/.mozilla/firefox/.... But I don't see the cookie set by my code in the file 'cookies.txt'. So where is it?

Thank you all for your help.
 
I know what's the problem now.

Code:
  my $ck = new CGI::Cookie(-name=>$cookieName,-value=>'');
  print $q->header(-cookie=>$ck);

The above code still sets a cookie, but with a NULL value. Therefore, I can not use
Code:
if(defined($cookies{$cookieName}))

alone to test if a cookie exists. I should also check the value of this cookie.
 
Good job :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top