Cannot set cookie with Perl

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,
-------------START
#!/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',
      -path=>'/',-domain=>'192.168.1.10');

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



LVL 3
yuhannlAsked:
Who is Participating?
 
andyj_tamarConnect With a Mentor Commented:
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.
0
 
yuhannlAuthor Commented:
Easy solution!
For some reason where ever I look, I can't find that solution.

Well done ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.