Solved

Cookie problem

Posted on 1998-09-20
9
166 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
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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

12 Experts available now in Live!

Get 1:1 Help Now