Re: perl - cookies

  • From: "Lamar Upshaw" <lupshaw@xxxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 26 Jun 2008 10:48:05 -0700

Thanks for your advice. I'm actually using a web host, which runs on linux, and i'm uploading my scripts to the server, and then trying them out by going to the web URL. All of my other perl scripts, including ones that deal with the MySQL database are working wonderfully. It's only the ones dealing with cookies that are coming up with 500 internal server errors.


BTW, where can I learn more about this frame work thing which will allow authorization and authentication usage through session cookies?

With All Respect,
Upshaw, Lamar T

----- Original Message ----- From: "Octavian Rasnita" <orasnita@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Wednesday, June 25, 2008 10:26 PM
Subject: Re: perl - cookies


Hi,

When you define cookies, you also need to specify the domain for which the cookie is valid, and that domain name should include at least 2 dots. You may also include the path but I don't know if it is required.

So in your cookie definition, you need to add:

-domain => '.blabla.com', -path => '/'

Then define the domain blabla.com in the file
c:\windows\system32\drivers\etc\hosts

where you need to put the following line:

127.0.0.1 www.blabla.com

You also need to create a virtual host for the site www.blabla.com exactly like for localhost.

The specifications say that the cookies are valid only for domains that contain at least 2 dots, because otherwise the sites on the domains like .co.uk could get the cookies for other sites from those domains.

I don't know, but it might also work without creating a new domain by using the loopback IP address directly, and accessing http://127.0.0.1/ instead of localhost.

But you may also need to add the 127.0.0.1 domain in the cookie definition.

It is very good to make programs like these 2 programs you've made, because this way you can understand much better how the web works, however, if you want to create web sites, you won't need to print html code in your perl scripts, because you can use templates, or even better, you could use frameworks like Catalyst, CGI::Application or others. Those frameworks create sessions that use cookies, can access databases easier, do authorization and authentication, use templates easier, can compress the content with gzip very easy and much many other things.

Octavian

----- Original Message ----- From: "Lamar Upshaw" <lupshaw@xxxxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, June 26, 2008 6:27 AM
Subject: perl - cookies


I'm playing with perl using cookies again, trying to brush up on what I once
knew. *smile* Below are the codes in two files.  Both are giving me an
internal server error (500), but I can't figure out what small thing i'm
missing, which I should know. lol  The two files are cookiesave.pl, and
cookieget.pl.  They are pretty self explanitory. If someone could either
point out my mistake, or point me in the right direction for tutorial help,
I would be very grateful.

cookiesave.pl
#!/usr/bin/perl
use strict;
use CGI;
my $query = new CGI;
my $username="tigger";
#Define the cookie content
my $cookie = $query->cookie(-name => 'username',-value => $username,-expires
=> '+90d');

#set the cookie in the header
$query->header(-cookie => $cookie);
print <<EOF;
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
<title>
cookie test part 1
</title>
</head>
<body>
<center>
<h1>Saved cookie</h1>
<p>
The Cookie should be saved.
<br />
<a href="cookieget.pl">
Click here to see if the cookie info can be retrieved.
</a>
</p>
</center>
</body>
</html>
EOF


cookieget.pl
#!/usr/bin/perl
use strict;
use CGI;
my $query = new CGI;
my $username= $query->cookie('username');
$query->header;
print <<EOF;
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
<title>
cookie test part 2
</title>
</head>
<body>
<center>
EOF
if (defined($username))
{
print <<EOF;
<h1>Retrieved cookie</h1>
<p>
The Cookie retrieved has the username: $username.
</p>

EOF
}
else
{
print <<EOF;
<h1>no cookie found</h1>
<p>
There was no cookie found. please try rewriting part 1 of the cookie test.
</p>
EOF
}
print <<EOF;
</center>
</body>
</html>
EOF


Thanks again for any and all assistance.

With All Respect,
Upshaw, Lamar T

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: