Solved

Cookie problem

Posted on 1998-09-20
9
177 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 138
hard perl script 16 154
Perl 101 11 72
Perl script to delete older files 6 73
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

863 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

21 Experts available now in Live!

Get 1:1 Help Now