Solved

How to write/read a cookie ?

Posted on 2002-06-29
7
262 Views
Last Modified: 2012-05-05
I am trying to write and read a cookie but running into some problem. If I run the same script (cookies.pl) locally on my PC, I can write cookie with domain='127.0.0.1' but if I replace domain with '.webrachna.com' and run it on webrachna.com, it does not work. Reading back the cookie is also not working. Could someone please help ? Here are the exact details:

~/www/cookies.html:
-------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
   <head>
      <title>Writing A Cookie To The Client Computer</title>
   </head>
<body>
   <form method = "post" action = "/cgi-bin/cookies.pl">
      <strong>name:</strong><br>
      <input type = "text" name = "name"><br>
      <input type = "submit" value = "Write Cookie">
   </form>

   <form method = "post" action= "/cgi-bin/read_cookies.pl">
      <input type = "submit" value = "Read Cookie">
   </form>
</body>
</html>

~/www/cgi-bin/cookies.pl:
-------------------------
#!/usr/bin/perl -w

use CGI qw/:standard/;

my $name = param(name);

$expires = -1;
$path = "/cgi-bin/";
# $server_domain = "127.0.0.1"; # works locally
$server_domain = ".webrachna.com";  # does not work

print "Set-Cookie: ";
print "Name", "=", $name, "; expires=", $expires,
      "; path=", $path, "; domain=", $server_domain, "; secure", "\n";

print header;
print "<BODY BACKGROUND = \"/images/back.gif\">";
print "<BASEFONT FACE = \"ARIAL,SANS-SERIF\" SIZE = 3>";
print "The cookie has been set with the following data:";
print "<BR><BR>";
print "<FONT COLOR=BLUE>Name:</FONT> $name <BR>";

~/www/cgi-bin/read_cookies.pl:
------------------------------
#!/usr/bin/perl -w

use CGI qw/:standard/;

print header;
print "<BODY>";
print "<STRONG>The following data is saved in a cookie on your ";
print "computer.</STRONG><BR><BR>";

my %cookie = &readCookies;

print ("<TABLE ",
       "BORDER = \"5\" ",
       "CELLSPACING = \"0\" ",
       "CELLPADDING = \"10\">");

foreach $cookie_name (keys %cookie)
{
   print "<TR>";
   print "   <TD BGCOLOR=#AAAAFF>$cookie_name</TD>";
   print "   <TD BGCOLOR=#AAAAAA>$cookie{$cookie_name}</TD>";
   print "</TR>";
}
print "</TABLE>";

sub readCookies
{
   my @cookie_values = split(/; /, $ENV{'HTTP_COOKIE'});

   foreach (@cookie_values) {
      my ($cookie_name, $cookie_value) = split ( /=/, $_ );
      $cookies{$cookie_name} = $cookie_value;
   }
   return %cookies;
}
0
Comment
Question by:nkapoor
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:Tsvetomir
ID: 7119678
You  can't do this!

You can not set cookies for other domans.

That's the idea of of cookies - the domain has to contain the domain from the request.
(if the request is  http://webrachna.com/mysctipt.cgi with $server_domain = ".webrachna.com" you can set cookie for www.webracha.com, www2.webracha.com and s.o. but not for www.otherdomain.com)

That's why it's not working you are trying to set a cookie for .webrachna.com  from 127.0.0.1.
0
 

Author Comment

by:nkapoor
ID: 7120078
I think I was able to understand that part but the problem is when I install it on webrachna.com and put domain as .webrachna.com instead of 127.0.0.1, even then it is not working.
0
 

Author Comment

by:nkapoor
ID: 7120843
Ok, I just figured out why I was not able to write the cookie. I was using http://webrachna.com/cookies.html as the URL instead of http://www.webrachna.com/cookies.html ! I did not know omitting 'www.' could cause that problem. Anyway, I still can not read the cookie back. Do you think you could could help me there ?

I am using read_cookies.pl code for which is also given above.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
Tsvetomir earned 100 total points
ID: 7121225
hmm... interesting...

Iwas just went to your site and everething was fine
I entered my name cookie was set and there was no problem reading it (The name apears in blue box).
What do you mean not working.

But .. since my first look to your script I'm wondering the you do the things.
Firs becouse of the " " everywhere when you have so many ways to do it more readable
and second:  you use CGI.pm for the input form values but not for reading / writng cookies -  WHY ??? it's ery convinient !
0
 

Expert Comment

by:Pete_C
ID: 7121333
It works for me also. I suspect it is a browser issue. Internet Explorer allows you to specify different behaviour for different zones. Your local IP is probably assigned to the local intranet zone, and the remote URL to the internet zone. Check that the cookie settings for both these zones allow cookies to be set.
0
 

Author Comment

by:nkapoor
ID: 7121761
Yep, it is working fine now. Thanks for all your help.
0
 
LVL 3

Expert Comment

by:Tsvetomir
ID: 7121951
#!/usr/bin/perl
use CGI qw/:standard/;
use CGI::Cookie;

print header, start_html("Title");

print qq{
<BODY>
  <STRONG>
    The following data is saved in a cookie on your computer.
  </STRONG>
  <BR><BR>
};

my %cookie = fetch CGI::Cookie;

print qq{ <TABLE BORDER = "5" CELLSPACING ="0" CELLPADDING = "10">};

foreach $cookie_name (keys %cookie) {
  print qq{
      <TR>
        <TD BGCOLOR=#AAAAFF>$cookie_name</TD>
        <TD BGCOLOR=#AAAAAA>$cookie{$cookie_name}</TD>
      </TR>
  };
}
print "</TABLE>";
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

861 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

25 Experts available now in Live!

Get 1:1 Help Now