Improve company productivity with a Business Account.Sign Up

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

How to write/read a cookie ?

I am trying to write and read a cookie but running into some problem. If I run the same script ( locally on my PC, I can write cookie with domain='' but if I replace domain with '' and run it on, it does not work. Reading back the cookie is also not working. Could someone please help ? Here are the exact details:

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

   <form method = "post" action= "/cgi-bin/">
      <input type = "submit" value = "Read Cookie">

#!/usr/bin/perl -w

use CGI qw/:standard/;

my $name = param(name);

$expires = -1;
$path = "/cgi-bin/";
# $server_domain = ""; # works locally
$server_domain = "";  # 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 "The cookie has been set with the following data:";
print "<BR><BR>";
print "<FONT COLOR=BLUE>Name:</FONT> $name <BR>";

#!/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;
  • 3
  • 3
1 Solution
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 with $server_domain = "" you can set cookie for, and s.o. but not for

That's why it's not working you are trying to set a cookie for  from
nkapoorAuthor Commented:
I think I was able to understand that part but the problem is when I install it on and put domain as instead of, even then it is not working.
nkapoorAuthor Commented:
Ok, I just figured out why I was not able to write the cookie. I was using as the URL instead of ! 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 code for which is also given above.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 for the input form values but not for reading / writng cookies -  WHY ??? it's ery convinient !
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.
nkapoorAuthor Commented:
Yep, it is working fine now. Thanks for all your help.
use CGI qw/:standard/;
use CGI::Cookie;

print header, start_html("Title");

print qq{
    The following data is saved in a cookie on your computer.

my %cookie = fetch CGI::Cookie;

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

foreach $cookie_name (keys %cookie) {
  print qq{
        <TD BGCOLOR=#AAAAFF>$cookie_name</TD>
        <TD BGCOLOR=#AAAAAA>$cookie{$cookie_name}</TD>
print "</TABLE>";
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now