Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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)
Thanks.
0
Gadi031698
Asked:
Gadi031698
1 Solution
 
Gadi031698Author Commented:
Adjusted points to 50
0
 
binkzzCommented:
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
 
thoellriCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
b2piCommented:
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
 
Gadi031698Author Commented:
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
 
binkzzCommented:
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
 
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...
0
 
binkzzCommented:
#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
 
binkzzCommented:
i mean perl and programming language even
0

Featured Post

Independent Software Vendors: 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!

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