Cookie problem

I have to set a cookie in the client's browser when executing a CGI.
I tried using the "setCookie" function, but it didn't work.
Does anyone have an idea (an intermediate web page is not acceptable)
Who is Participating?
Jesus, people are still hacking their own HTTP/HTML-code, when there is ready-made stuff out there. Check out from your nearest CPAN site (go to and then click on CPAN to find the nearest site). (latest version 2.42) has built-in capabilities for cookies and all you have to do is set them at the right place in your script or evaluate them. Here is an example from one of my perl-scripts:

This is where the cookie-values get evaluated:

    # Check if there is a cookie
    $prefs = $query->cookie('SBrowserPrefs');
    if ($prefs) {
        # yes - there is - get user preferences
        ($cookie_version,$pref_v, $pref_priv, $pref_priv_v, $pref_symbols) = split(/:/, $prefs);

And this is further down the code where I set the new cookie (or the cookie is set the first time):

# now make new cookie
$cookie1 = $query->cookie(-name=>'SBrowserPrefs',
                         -domain=>'<domain name omitted>',
                         -value=> "$cookie_version:$pref_v:$pref_priv:$pref_priv_v:$pref_symbols",

Read the documentation that comes with and it'll tell you how to handle the yummy cookies.

Gadi031698Author Commented:
Adjusted points to 50
You have to set the cookie at the beginning of the page,
when you do the Content type bit,

so for example:

print "Content-type: text/html\n";
print "Set-Cookie: blah blah";

or use a nice little sub:

sub setcookie
  # end a set-cookie header with the word secure and the cookie will only
  # be sent through secure connections
  local($name, $value, $expiration, $path, $domain, $secure) = @_;

  print "Set-Cookie: ";
  if($expiration eq "NONE")
    print ($name, "=", $value, "; path=", $path, "; domain=", $domain, ";", $secure, "\n");
    print ($name, "=", $value, "; expires=", $expiration,"; path=", $path, "; domain=",
    $domain, "; ", $secure, "\n");

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Gadi, you've got at least three questions which are locked, and
waiting for your response... How about answering some of them before
asking more?
Gadi031698Author Commented:
As far as I checked now - only this question is locked, and another one is unlocked (related to Perl2Exe).
But thanks.

Isn't it possible to set a cookie not at the beginning of a page, but at the end of processing one?
My problem is that I would like to set a "security cookie" to prevent accessing a page directly, but it should be done after the login phase, which is the previous page.
Thank you.
It is only possible to send a cookie at the beginning of the page;
If you want to do it after a login, you'd have to have the program
reload the page and send the secure cookie

Gadi031698Author Commented:
Do you mean reload the first (login) page?
I think it is not acceptable.
Do you actually mean that there is no solution to the problem? That's an answer, too...
#It is very possible to get round it;
#with a cgi, when your page loads up it checks for a cookie; ie

Print "Content-type: text/html\n";

$method = $ENV{"REQUEST_METHOD"};

if ($method eq "POST") {
  print "\n";
elsif (&validcookie) {
  print "\n";
else {
  print "\n";

  $buffer = `cat login.html`;
  print $buffer;

#to give you a simple example
#there's always a way around problems in unix, that's why it is
#such a good operating system.
i mean perl and programming language even
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.

All Courses

From novice to tech pro — start learning today.