×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Perl: http cookies
2

Perl: http cookies

Perl: http cookies

(OP)
What can you people tell me about cookies with perl?
I would like to know how to set them, and how to read them, as well as all of the parameters (ie exp date, domain, etc)

thanks in advance

theEclipse
eclipse_web@hotmail.com
robacarp.webjump.com
**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.

RE: Perl: http cookies

(OP)
does anybody want to help me?? please!!

theEclipse
eclipse_web@hotmail.com
robacarp.webjump.com
**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.

RE: Perl: http cookies

Are you using CGI.pm?......Yes?  Then,


#!/usr/local/bin/perl
use CGI;
$thisCGI = '/cgi-bin/examples/setCookie'; # change to your dir
$query = new CGI;

$cookThis = $query->cookie(-name=>'cookThis');
if ($cookThis)
    {
    $cookie = $query->cookie(-name=>"cookThis",
        -value=>',
        -expires=>"+1h");
    }
else
    {
    $cookie = $query->cookie(-name=>"cookThis",
        -value=>"Netscape Version",
        -expires=>"+1h");
    }

print $query->header(-cookie=>$cookie);
print "<BR>CookThis is $cookThis<BR>";
print $query->start_html(-title=>"UPLOAD THIS");
print $query ->start_multipart_form('POST',"$thisCGI");
print '<BR>',$query->submit('doWhat','cook');
print $query->end_form;
print $query->end_html;



see the CGI.pm docs for more details.....you can 'perldoc CGI.pm ' or see
http://stein.cshl.org/WWW/software/CGI/cgi_docs.html#cookies

'hope this helps...




keep the rudder amid ship and beware the odd typo

RE: Perl: http cookies

(OP)
thanks. but do you know how to do it without the module? I guess I could upload the module, but I don't really want to. I would like to keep the script's completly independant.

theEclipse
eclipse_web@hotmail.com
robacarp.webjump.com
**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.

RE: Perl: http cookies

Unfortunately, once I started using CGI.pm, I never went back.  My first attempts to use cookies were with the module, so I have never done them 'long hand'.  Sorry.  I would encourage you to think about getting the module.  I think it is important to write enought CGI stuff the long way( sans CGI.pm) to get an understanding of how it works and why, but, once that general understanding is acquired, the module makes for some real time savings.   Also, I don't think using the module will make your code much less portable.  Use of the CGI.pm module is ubiquitous.

Also, ' might look at "Creating cookies with PERL" section at http://tergestesoft.tripod.com/alabcook.htm




keep the rudder amid ship and beware the odd typo

RE: Perl: http cookies

You can download the zip file Perl Examples from this site. It has some good example scripts done in Perl that you can get some cookie ideas from. You will find that they are pretty easy.

http://www.cookwood.com/perl/examples/Perl_Examples.zip

Hope this helps.

-Vic

RE: Perl: http cookies

2
What is a cookie?

A cookie is a chunk of information that you're allowed to save on a client computer.
Normally, for security reasons, you are not allowed access to computers that visit your web page. However, cookies are an exception that allow you to save information on someone's computer in a structured and somewhat limited fashion.

What does a cookie look like?

A cookie consists of a "name=value" pair, such as
USER_ID = 1234

In this case, the cookie is named 'USER_ID', and the value is '1234'.

You can also save information about the cookie, such as

    The cookie's expiration date and time
    The valid domain for the cookie
    The valid server path for the cookie
    Whether or not to use a secure channel for the cookie

A fully specified cookie looks something like this:
TEST_COOKIE=Famous%20Amos; domain=.mydomain.com; path=/; expires=Sat, 11-Mar-2000 22:52:37 GMT; secure


Cool. Now that you know what a cookie looks like, the next step is to show you how to set and get them from your scripts. Don't worry, its easy.



set_cookie


    set_cookie steps through the cookie associative array and sets what ever name/value pairs it finds. This means, in order to set a cookie you have to do something like this:
    $cookie{'color'} = blue;
    &set_cookie($expiration,$domain,$path,$secure);

    The arguments are as follows:
          $expiration
          Expiration date of the cookie, expressed in unix time format (seconds since Jan 1, 1970).
          All expiration dates are in Greenwich Mean Time. (Keep this in mind.)
          If you don't want to set any expiration, then use $expiration = "-1".
          If you want a "permanent" cookie (it lasts until 12/31/1998) then set $expiration = ""

          $domain
          Domain that the cookie data can be sent back to. Ordinarily you would leave this blank or
          $domain = "". This allows only you to read the cookie.

          $path
          Any file contained within $path (and it's subdirectories) can retreive the data contained within the cookie. For all pages on your server use "/", however in case of a subdirectory "/subdirectory".

          $secure
          Secure can either be equal to 0 or 1. If it's not a secure cookie, you can either leave it out, or $secure = 0. If for some reason you want to use secure cookies, set $secure = 1.

Set_cookie must be called before you print "Content-type:
    text/html\n\n";


sub set_cookie {
# $expires must be in unix time format, if defined. If not defined it sets the expiration to December 31, 1999.
# If you want no expiration date set, set $expires = -1 (this causes the cookie to be deleted when user closes
# his/her browser).

    local($expires,$domain,$path,$sec) = @_;
        local(@days) = "Sun","Mon","Tue","Wed","Thu","Fri","Sat");
        local(@months) = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    local($seconds,$min,$hour,$mday,$mon,$year,$wday) = gmtime($expires) if ($expires > 0); #get date info if expiration set.
    $seconds = "0" . $seconds if $seconds < 10; # formatting of date variables
    $min = "0" . $min if $min < 10;
    $hour = "0" . $hour if $hour < 10;
    local(@secure) = ("","secure"); # add security to the cookie if defined. I'm not too sure how this works.
    if (! defined $expires) { $expires = " expires\=Fri, 31-Dec-1999 00:00:00 GMT;"; } # if expiration not set, expire at 12/31/1999
    elsif ($expires == -1) { $expires = "" } # if expiration set to -1, then eliminate expiration of cookie.
    else {
        $year += 1900;
        $expires = "expires\=$days[$wday],
$mday-$months[$mon]-$year $hour:$min:$seconds GMT; "; #form expiration from value passed to function.
    }
    if (! defined $domain) { $domain = $ENV 'SERVER_NAME'}; } #set domain of cookie. Default is current host.
    if (! defined $path) { $path = "/"; } #set default path = "/"
    if (! defined $secure) { $secure = "0"; }
    local($key);
    foreach $key (keys %cookie) {
        $cookie{$key} =~ s/ /+/g; #convert plus to space.
        print "Set-Cookie: $key\=$cookie{$key};
$expires path\=$path; domain\=$domain; $secure[$sec]\n";
                #print cookie to browser,
                #this must be done *before*    you print any content type headers.
    }
}




get_cookie

get_cookie reads the cookies set to the script by the browser. All cookie information is available though the environmental variable HTTP_COOKIE.

To read the cookies, simply call the subroutine "get_cookie" from anywhere in your script.

Cookies are stored in an associative array like so:
    $cookie{name} = value

sub get_cookie {
    local($chip, $val);
    foreach (split(/; /, $ENV{'HTTP_COOKIE'})) {
            # split cookie at each; (cookie format is name=value; name=value; etc...)
            # Convert plus to space (in case of encoding (not necessary, but recommended)
        s/\+/ /g;
            # Split into key and value.
        ($chip, $val) = split(/=/,$_,2); # splits on the first =.
            # Convert %XX from hex numbers to alphanumeric
        $chip =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
        $val =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
            # Associate key and value
        $cookie{$chip} .= "\1" if (defined($cookie{$chip})); # \1 is the multiple separator
        $cookie{$chip} .= $val;
    }
}

RE: Perl: http cookies

(OP)
pump-

thanks, thats just what I was looking for! Awesome!

and thanks to all you guys who suggested other soloutions.

theEclipse
eclipse_web@hotmail.com
robacarp.webjump.com
**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.

RE: Perl: http cookies

(OP)
pump-

Oh wait, I do have three questions:1) what IS a secure cookie?
does it limit it to the specifyed domain?

2)do you have a sub to delete a cookie?

and 3) what should I put for the exipration date, if it is a sesson only cookie? (like a shopping cart?)

thanks again

theEclipse
eclipse_web@hotmail.com
robacarp.webjump.com
**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.

RE: Perl: http cookies

Eclipse:

1) Yes, a cookie can be limited to a specific domain or a specific path on a domain.

2) Yes:

&delete_cookie("name1", "name2");

delete_cookie will delete cookies of the given names.  It does so by setting the expiration date in
the past so that the browser will remove the cookie from it's memory.

sub delete_cookie {
  # to delete a cookie, simply pass delete_cookie the name of the cookie to delete.
  # you may pass delete_cookie more than 1 name at a time.
  local(@to_delete) = @_;
  local($name);
  foreach $name (@to_delete) {
   undef $cookie{$name}; #undefines cookie so if you call set_cookie, it doesn't reset the cookie.
   print "Set-Cookie: $name=deleted; expires=Thu, 01-Jan-1970 00:00:00 GMT;\n";
    #this also must be done before you print any content type headers.
  }
}


3) Well if you want your cookie to be deleted after the session ends then you must set the expiration date to a blank date. eg:

TEST_COOKIE=Famous%20Amos; domain=.mydomain.com; path=/; secure


Hope this help

RE: Perl: http cookies

(OP)
thats not what I asked. I asked what is a secure cookie. in other words, how is it different from a non-secure cookie?

theEclipse
eclipse_web@hotmail.com
robacarp.webjump.com
**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.

RE: Perl: http cookies

If a cookie is secure defines whether or not you want to restrict the transmission of the cookie to a secure server. If this flag is set to '1', by default the cookies you create will only be sent to secure servers by the client.

A cookie is marked secure by putting the word secure at the end of the request. A secure cookie will be sent only to a server offering HTTPS (HTTP over SSL).

Hope this helps

RE: Perl: http cookies

(OP)
aaahhhhhh. I think that answeres all of my questions.

theEclipse
eclipse_web@hotmail.com
robacarp.webjump.com
**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close