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

CGI vs. CGI::Cookies

Status
Not open for further replies.

Haazi2

Programmer
Aug 25, 2000
51
0
0
US
I've looked at the docs for CGI and CGI::Cookie but I'm confused about which one to use. Well, here's what I'm trying to do. I have a script that will force a login if a cookie does not exist or the expiration date is invalid. I want to reset the expiration date to an invalid date to force a login. Should I use CGI or CGI::Cookie. I know with CGI you can create cookies but I've seen no examples about deleting a cookie or modifying one. Thanks for any help in advance. A small example would help also.
 
The following does not delete the cookie, but it sets the value to null and 'Some Cookie value here' alternately. Maybe, it can be modified to do what you want. Feel free to ask if you need clarification. You can copy/paste this into a file in your cgi-bin and it will produce a page with a button that switches the value of the the cookie between null and 'Some Cookie value here'.


Code:
#!/usr/local/bin/perl
use CGI;
$query = new CGI;
$thisCGI = $query->url();

$cookThis = $query->cookie(-name=>'cookThis');
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;

'hope this helps...




keep the rudder amid ship and beware the odd typo
 
Thanks goBoating! That helps a lot. I also saw a module called HTTP::Cookies. Any comment on using that one?
 
I don't personally know that much about cookies, but it seems to me that if you want to cut through the confusion of these multiple modules, just find the cookie specification docs at w3.org and learn to do it manually. I do all of my CGI and HTML manually and it is much easier than using any of the modules and learning all of their object-oriented functions. Much more powerful this way too because you're not limited by what someone else decided to implement or not.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Haazi2,
' Never messed with HTTP::Cookies so I can't comment on it. I do know that CGI.pm will do the job as shown above.

Tom,
I agree with you to a point about manually generating the code. However, I have found that there are quite a few things that I do repeatedly-repeatedly that CGI.pm helps with. I use the module to build the frequently used parts of pages and then mix in manually generated stuff as needed. It cuts down on the amount of code I have to write. Basically, I use the module for what its good for. There is some significant economy of effort gained. Additionally, CGI.pm provides a function based interface that is not object oriented. If you don't like the object syntax, many of the methods can be called just like functions.

Good Luck haazi2, let us know if you need more......




keep the rudder amid ship and beware the odd typo
 
Thanks goBoating and tanderso!

I'll use the CGI.pm module but will aslo look at doing things by hand also. Doing it by hand can help me further increase my perl knowledge. Again, Thanks guys!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top