Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

CGI Cookie creation problem

by Nicko2004 (Initiate)
on Jun 19, 2012 at 12:42 UTC ( #977058=perlquestion: print w/ replies, xml ) Need Help??
Nicko2004 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I previously extensively programmed in CGI and Perl about 10 years ago. I am just back to it building an e-commerce website for a new business venture. I still have my old code but trying a write a simple cookie has thrown me. The below CGI prograpm runs fine and redirects correctly to the required Perl program but it does not create the cookie as I would expect. I know I am rusty but I have agonised for the past few hours over it. Any help as to what I have done wrong? A big thanks in advance. My code is as follows:

use CGI () # $mc_form_member_name is extracted from the Member Database Table $mc_cookie_domain_name = ".sample.com"; $mc_cookie_expiration_time = "+2h"; $mc_cookie_name = "Order_Cookie"; $mc_cookie_value = $mc_form_member_name; my $cgi = CGI->new(); my $cookie = $cgi->cookie(-name => $mc_cookie_name, -value => $mc_cook +ie_value, expires => $mc_cookie_expiration_time, domain => $mc_cookie +_domain_name); print $cgi->redirect(-url => 'https://www.sample.com/cgi-bin/confirm_a +ccount_2.pl', -cookie => $cookie); #End of Create Cookie

Comment on CGI Cookie creation problem
Download Code
Re: CGI Cookie creation problem
by muba (Priest) on Jun 19, 2012 at 13:03 UTC

    Couple of things that stand out after toying with your code for less than five minutes:

    1. Missing semicolon at the end of the first line
    2. Does the cookie method accept arguments named expires or domain, or should they read -expires and -domain?
    3. Try adding this line just after the first: my $mc_form_member_name = "";. See what happens now.
    4. Now change that line to say my $mc_form_member_name = undef; and see what happens now.

      Hi, Sorry I did miss a few things. I updated my code as what is listed below but still no cookie is created. Thanks for your reply!

      use CGI (); #Create Cookie $mc_cookie_domain_name = ".sample.com"; $mc_path_script = "/"; $mc_cookie_expiration_time = "+2h"; $mc_cookie_name = "Sample_Cookie"; #$mc_cookie_value = $mc_form_member_name; $mc_cookie_value = "Logged In"; my $cgi = CGI->new(); my $cookie = $cgi->cookie(-name => $mc_cookie_name, -value => $mc_cook +ie_value, -expires => $mc_cookie_expiration_time, -domain => $mc_cook +ie_domain_name); print $cgi->redirect(-url => 'https://www.sample.com/cgi-bin/confirm_a +ccount_2.pl', -cookie => $cookie); #End of Create Cookie

        Are you sure? When I run this script from the command line it gives me pretty much what you'd expect:

        G:\>type x.pl use CGI (); #Create Cookie $mc_cookie_domain_name = ".sample.com"; $mc_path_script = "/"; $mc_cookie_expiration_time = "+2h"; $mc_cookie_name = "Sample_Cookie"; #$mc_cookie_value = $mc_form_member_name; $mc_cookie_value = "Logged In"; my $cgi = CGI->new(); my $cookie = $cgi->cookie(-name => $mc_cookie_name, -value => $mc_cook +ie_value, -expires => $mc_cookie_expiration_time, -domain => $mc_cookie_domain_n +ame); print $cgi->redirect(-url => 'https://www.sample.com/cgi-bin/confirm_a +ccount_2.p l', -cookie => $cookie); #End of Create Cookie G:\>perl x.pl Status: 302 Found Set-Cookie: Sample_Cookie=Logged%20In; domain=.sample.com; path=/; exp +ires=Tue, 19-Jun-2012 16:45:45 GMT Date: Tue, 19 Jun 2012 14:45:45 GMT Location: https://www.sample.com/cgi-bin/confirm_account_2.pl G:\>
Re: CGI Cookie creation problem
by Anonymous Monk on Jun 19, 2012 at 13:14 UTC
Re: CGI Cookie creation problem
by daxim (Chaplain) on Jun 19, 2012 at 13:24 UTC
    $mc_form_member_name is undefined, thus the $cookie object is not created, thus the HTTP header created by the redirect method does not contain the Set-Cookie line. All your programs should include use strict; use warnings FATAL => 'all'; to abort on such common mistakes.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://977058]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-07-13 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (245 votes), past polls