Solved

Cookie problem

Posted on 1998-09-20
9
198 Views
Last Modified: 2010-03-05
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)
Thanks.
0
Comment
Question by:Gadi031698
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 

Author Comment

by:Gadi031698
ID: 1204900
Adjusted points to 50
0
 
LVL 4

Expert Comment

by:binkzz
ID: 1204901
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");
  }
  else
  {
    print ($name, "=", $value, "; expires=", $expiration,"; path=", $path, "; domain=",
    $domain, "; ", $secure, "\n");
  }
}

0
 
LVL 5

Accepted Solution

by:
thoellri earned 50 total points
ID: 1204902
Jesus, people are still hacking their own HTTP/HTML-code, when there is ready-made stuff out there. Check out CGI.pm from your nearest CPAN site (go to www.perl.com and then click on CPAN to find the nearest site). CGI.pm (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>',
                         -path=>'/sbrowser',
                         -value=> "$cookie_version:$pref_v:$pref_priv:$pref_priv_v:$pref_symbols",
                         -expires=>'+1y');

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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:b2pi
ID: 1204903
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?
0
 

Author Comment

by:Gadi031698
ID: 1204904
b2pi,
As far as I checked now - only this question is locked, and another one is unlocked (related to Perl2Exe).
But thanks.

thoellri/binkzz,
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.
0
 
LVL 4

Expert Comment

by:binkzz
ID: 1204905
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

Binkzz
0
 

Author Comment

by:Gadi031698
ID: 1204906
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...
0
 
LVL 4

Expert Comment

by:binkzz
ID: 1204907
#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";
  getwebdata();
}
elsif (&validcookie) {
  sendcookie();
  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.
0
 
LVL 4

Expert Comment

by:binkzz
ID: 1204908
i mean perl and programming language even
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

751 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