Cannot set cookie with Perl

Posted on 2005-05-01
Last Modified: 2006-11-18
After reading many sources in regards to setting/reading cookies, I'm convinced that I should pop the question here instead of stuffing around for hours.

Simple thing I want to do, set cookie, and be able to read it. My code are as follow,
#!/usr/bin/perl -d

# using strict pragma, CGI and DBI classes
use strict;
use CGI;
use DBI;

# Declaring variables & objects that will be used.
my $obj = new CGI;
my $params = $obj->Vars;
my $fut_time;
my $cookie;
my $cc;
# Parsing information from form
my $username = $params->{'username'};
my $password = $params->{'password'};

# Connecting to the database
my $db = DBI->connect('dbi:mysql:database=WebMessage', 'root', '', );

# Preparing and executing query for checking username and password entered
my $qryCheckID = $db->prepare('SELECT Username, Password FROM Member WHERE Username = ? AND Password = ?');
$qryCheckID->execute($username, $password);

$cookie = $obj->cookie(-name=>'TEST', -value=>'FAMOUS', -expires=>'+24h',

print "Content-Type: text/html\n\n";
print header(-cookie=>$cookie);
----------- FINISH

Now, all I want to do is to be able to retrieve that cookie and put it into $cc.

However, after setting the cookie, I can't retrieve it. I'm suspecting that some dir permission not sete properly, or I simply haven't done it right.
If I check my browser's cookies, sites with cookies will work, but not my own. As if the Perl script can't "write" to it.

I'm running Gentoo 2005.0 / Firefox 1.03 / Perl 5
htdocs = /var/www/localhost/htdocs/yuhannl
cgi-bin = /var/www/localhost/htdocs/yuhannl/cgi-bin
firefox profiles = %home%/.mozilla/firefox

Question by:yuhannl
    LVL 1

    Accepted Solution

    I don't think that you have set the cookie correctly

    replace these two lines:

    print "Content-Type: text/html\n\n";
    print header(-cookie=>$cookie);

    with this:

    print $obj->header(-cookie=>$cookie); # prints HTTP header containing cookie
    Your first line will send the HTTP header (the two new lines '\n\n' denote the end of the HTTP header). The next line where the cookie is printed will then be intepreted by the browser as the content of the page instead of as a cookie. You need to send the cookie as part of the HTTP header - the easiest way to do this is let the CGI module print the http header.

    Hope this makes sense.
    LVL 3

    Author Comment

    Easy solution!
    For some reason where ever I look, I can't find that solution.

    Well done ;)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now