Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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 = ""; $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 => '', -cookie => $cookie); #End of Create Cookie

Replies are listed 'Best First'.
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 = ""; $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 => '', -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 use CGI (); #Create Cookie $mc_cookie_domain_name = ""; $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 => ' +ccount_2.p l', -cookie => $cookie); #End of Create Cookie G:\>perl Status: 302 Found Set-Cookie: Sample_Cookie=Logged%20In;; path=/; exp +ires=Tue, 19-Jun-2012 16:45:45 GMT Date: Tue, 19 Jun 2012 14:45:45 GMT Location: G:\>
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.
Re: CGI Cookie creation problem
by Anonymous Monk on Jun 19, 2012 at 13:14 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://977058]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2018-02-24 06:42 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.