Re: perl - cookies

  • From: "Octavian Rasnita" <orasnita@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 26 Jun 2008 08:26:22 +0300

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

Other related posts: