• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

Problem setting cookies w/ PERL

Using a UNIX web hosting service that claims to have CGI.pm installed, I cannot set a cookie using:

$cookie = $q->cookie(-name=>"login_tracker",
                         -value=>$rowdata[0],
                         -domain=>".domainname.com",
                         -expires=>"+1y",
                         -path=>"/subdirectory");

print "Set-Cookie: $cookie\n";

The error I get fed back says that it cannot "call method 'cookie' on line [1 in my example above]

What is going on?

I have use CGI; defined at the beginning of the script. The code above is set within an If clause.

Rusty
0
Rusty20009
Asked:
Rusty20009
  • 7
  • 6
  • 2
1 Solution
 
TintinCommented:
Change

print "Set-Cookie: $cookie\n";


to

print $q->header(-cookie=>$cookie);
0
 
Rusty20009Author Commented:
I had already tried that, but that again is not the line of code that is causing the error. The first occurance of the word "cookie" is causing the browser to shoot back an error message saying that it is an undefined value....

It is not my variable $cookie, which I now have set as $mycookie but rather the command word cookie directly preceding the cookie's name...

0
 
cjmosCommented:
can we see the rest of the code?

Is any other cgi code working?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Rusty20009Author Commented:
Yes, but of course. Here is the full code. Everything works as it should when the cookie code is not included.



#!/usr/local/bin/perl -w
use CGI qw(:cgi-lib :standard);
use CGI::Carp qw(fatalsToBrowser);
use DBI;

my ($userid, $password, $regdate, $dbh, $dbuser, $dbname, $dbhost, $dbport, $dbpass, $sql, $sth, $rv);
my ($sec, $min, $hour, $wday, $month, $year, $mday, $yr, @rowdata, $mycookie, $q);

$dbuser = "XXXXX";
$dbname = $XXXXXr;
$dbhost = "localhost";
$dbport = 3306;
$dbpass = "XXXXX";
$useridcheck = param('userid');
$passwordcheck = param('password');
($sec,$min,$hour,$wday,$month,$year,$mday)=localtime(time());

$month="0".$month if ($month)<10;
#make month in 2 digits if it is less than 2

$wday="0".$wday if ($wday)<10;
#make date in 2 digits if it is less than 2

$yr = $year + 1900;
#make year in 4 digits
 
$regdate=$yr."-".$month."-".$wday;
#concatenate the date in yyyy-mm-dd format.


if ($useridcheck !~ /^[\w .!?-@]/)
{
$w1 = '<font class="warning">Please Limit your imput to letters, numbers and the the following special keys: _ ! . ? - and \&amp\; </font>';
}
elsif ($useridcheck eq "")
{
$w1 = '<font class="warning">Please enter in your e-mail address</font>';
}
elsif ($useridcheck !~ /.*.@.*.\..*./)
{
$w1 = '<font class="warning">Please enter in a proper e-mail address</font>';
}

if ($passwordcheck !~ /^[\w .!?-@]/)
{
$w2 = '<font class="warning">Please Limit your imput to letters, numbers and the the following special keys: _ ! . ? and - </font>';
}
elsif ($passwordcheck eq "")
{
$w2 = '<font class="warning">Please enter in a password</font>';
}


if (($useridcheck =~ /.*.@.*.\..*./) && ($passwordcheck ne /^[\w .!?-]/) && ($passwordcheck ne /^[\w .!?-]/))
{
## Connect to MySQL database
$dbh = DBI->connect("DBI:mysql:$dbname:$dbhost:$dbport", $dbuser, $dbpass)
      || die "Unable to connect to MySQL database!\n";
$sql = "select first, last, userid, password from registration where userid = '$useridcheck'";
$sth = $dbh->prepare($sql);
$rv = $sth->execute;
@rowdata = $sth->fetchrow_array;
$sth->finish;

if (($useridcheck =~ $rowdata[2]) && ($passwordcheck =~ $rowdata[3]))
{
$mycookie = $q-> cookie(-name=>"login_tracker",-value=>$rowdata[0],-domain=>".mydomainname.com",-expires=>"+1y",-path=>"/subdirectory");
print $q->header(-cookie=>$mycookie);

print "Content-type: text/html\n\n";
open (CGI, "../subdirectory/welcome.php") || die ("File not created");
close CGI;

print <<"CGI code";
      
      <html>
      <head>
      <title>Loading Results</title>
      <META HTTP-EQUIV="refresh" CONTENT="0; URL=XXXXX">
      </head>
      <body>
      </body>
      </html>
      
CGI code
}

else
{
print "Content-type: text/html\n\n";
open (CGI, ">../loginunsuccessful.htm") || die ("File not created");
print CGI << "HTML file";
      <html>
      <head>
      <title>Login Submission Unsuccessful</title>
      <link rel='stylesheet' rev='stylesheet' href='stylesheet.css' type='text/css'>
      </head>
            
      <body bgcolor=#E9E7DA>
      <center><br><br><br><br>
      <table width=650 border=0><tr><td>
      <font>The userid and password you entered does not match with our records, please try again!<br><br>
      <a href='login.htm' target='display'><i>login prompt</i></a>
      </font>
      </td></tr></table><br><br>
      </center>
      </body>
      </html>

HTML file
close CGI;
      
print <<"CGI code";
      
      <html>
      <head>
      <title>Loading Results</title>
      <META HTTP-EQUIV="refresh" CONTENT="0; URL=XXXXXXXXXX">
      </head>
      <body>
      </body>
      </html>
      
CGI code
}
}

else
{
print "Content-type: text/html\n\n";
open (CGI, "../login.htm") || die ("File not found");
close CGI;
      
print <<"CGI code";
      
      <html>
      <head>
      <title>Loading Results</title>
      <META HTTP-EQUIV="refresh" CONTENT="0; URL=XXXXXXXXXXXX">
      </head>
      <body>
      </body>
      </html>
      
CGI code
}
0
 
cjmosCommented:
You're not setting $q  

e.g:

my ($userid, $password, $regdate, $dbh, $dbuser, $dbname, $dbhost, $dbport, $dbpass, $sql, $sth, $rv);
my ($sec, $min, $hour, $wday, $month, $year, $mday, $yr, @rowdata, $mycookie);

my $q = new CGI;
0
 
cjmosCommented:
Notice that $q has also been taken out of

my ($sec, $min, $hour, $wday, $month, $year, $mday, $yr, @rowdata, $mycookie);
0
 
Rusty20009Author Commented:
Believe it or not, I had actually included that line of code in various attempts to debug the script. When I included that line, it spits back the following error:

Can't locate object method "new" via package "IO::Handle"

It was because of this error message and the "cookie" error message that before I came here to answer my question, I sent the web hosting service an inquiry as to whether they have CGI.pm installed (most sites claim that that module is necessary for such cookie code) -- and of course they claim that it is included in the service.

Any ideas?

Rusty

0
 
Rusty20009Author Commented:
If I add:

use IO::Handle;

In the list of modules to employ, it understands the command 'new' but then states that -

Can't locate object method "cookie" via package "GLOB"

,......
0
 
Rusty20009Author Commented:
Clarification --

A second opinion from my web hosting service --- their Unix boxes do not have CGI.pm installed, only windows...

I have assigned the points to CJMOS since I am sure that his response would have lead to the correct solution.

CJ -- If you are willing to help me, please let me know what I can do about setting cookies in the absense of the CGI.pm module....

Rusty
0
 
cjmosCommented:
Rusty, sorry i've been away for a while.

I don't know of any way of setting cookies without CGI off hand but i'm certain there's a way. I'll look into it for you.

0
 
cjmosCommented:
Incidently,

It's very odd for a Unix box not to have CGI installed...
0
 
cjmosCommented:
Rusty,


take a look at:
http://wp.netscape.com/newsref/std/cookie_spec.html

I'm guessing you'd need something alon the lines of

print "Set-Cookie: NAME='login_tracker'\; expires='+1y'\;path='/subdirectory'\;";

cj
0
 
Rusty20009Author Commented:
CJ - Thanks for the follow up. Very kind. It looks like it is going to work... I am not getting the same error messages.

Rusty
0
 
TintinCommented:
Rusty20009.

If your service provider has deleted the standard CGI.pm module that comes as part of the Perl distribution, then they have a *very* strange idea of supporting their clients.  I have no idea why they would want to go out of their way to break a standard Perl function.

Some general points on other matters.  The date code:

$sec,$min,$hour,$wday,$month,$year,$mday)=localtime(time());

$month="0".$month if ($month)<10;
#make month in 2 digits if it is less than 2

$wday="0".$wday if ($wday)<10;
#make date in 2 digits if it is less than 2

$yr = $year + 1900;
#make year in 4 digits
 
$regdate=$yr."-".$month."-".$wday;
#concatenate the date in yyyy-mm-dd format.


can be shortened to

($day,$month,$year) = (localtime)[3..5];
$regdate=sprintf("%d-%02d-%02d",$year,$month,$day);

or

use POSIX;

$regdate=("%Y-%m-%d",localtime);

You realise of course your email address check is extremely limited.

0
 
Rusty20009Author Commented:
Thank you for the extra pointers Tintin. Not having any academic background in the computer sciences, everything that I have done has been a learn as I go sort of thing. I do realize that my email check is limited --> again it is what I have been able to come up with on my own.

As for web hosting company --> it has been very frustrating. They have not been very supportive with feedback, nor well organized. So I finally gave up trying to get PERL cookies to work, and am now using PHP to set the cookies.

Rusty
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now