CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
persistent cookies

 



LanceLovesJia
New User

Mar 4, 2003, 1:34 AM

Post #1 of 5 (1083 views)
persistent cookies Can't Post

can anyone explain to me how i can retrieve persistent cookies from a user's web browser? i can't seem to get this to work

#- Set Cookie -----------------------------------------------------------------#
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: ";
print ($name, "=", $value, "; expires=", $expiration,
"; path=", $path, "; domain=", $domain, "; ", $secure, "\n");
}
#------------------------------------------------------------------------------#


#- Retrieve Cookies From ENV --------------------------------------------------#
sub getCookies {
# cookies are seperated by a semicolon and a space, this will split
# them and return a hash of cookies
local(@rawCookies) = split (/; /,$ENV{'HTTP_COOKIE'});
local(%cookies);

foreach(@rawCookies){
($key, $val) = split (/=/,$_);
$cookies{$key} = $val;
}

return %cookies;
}
#------------------------------------------------------------------------------#

# be sure to print a MIM type AFTER cookie headers and follow with a blank line
print "Content-type: text/html\n\n";

# this is the first thing the user sees in the browser
print "\nReload for Cookies:<BR>";
%cookies = &getCookies; # store cookies in %cookies

foreach $name (keys %cookies) {
print "\n$name = $cookies{$name}<br>";
}
#------------------------------------------------------------------------------#

this is taken from http://www.bewley.net/perl/

can anyone explain to me why my browser is nto displaying the cookies name and stuff?


davorg
Thaumaturge / Moderator

Mar 4, 2003, 6:18 AM

Post #2 of 5 (1076 views)
Re: [LanceLovesJia] persistent cookies [In reply to] Can't Post

The reason that you're not seeing the cookies seems to be because you're not calling the setCookie function anywhere.

Your bigger problem seems to be that you are using examples from a very outdated web site. You'd be better off using the cookie support in CGI.pm. See the documetation for details.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


LanceLovesJia
New User

Mar 4, 2003, 5:52 PM

Post #3 of 5 (1071 views)
Re: [davorg] persistent cookies [In reply to] Can't Post

thanx for the help


LanceLovesJia
New User

Mar 4, 2003, 7:29 PM

Post #4 of 5 (1067 views)
Re: [LanceLovesJia] persistent cookies [In reply to] Can't Post

i've gone through the cgi.pm module.. but there's a part which i need to ask about again.. how do we use the raw_cookies function?

$raw_cookie = $query->raw_cookie($ENV{'COOKIE'})

print $query->raw_cookie($self=>http('cookie'));


sorry im rather new at this.. but i cant seem to figure out what variables to pass into it and such..

thanks once again


davorg
Thaumaturge / Moderator

Mar 5, 2003, 1:45 AM

Post #5 of 5 (1063 views)
Re: [LanceLovesJia] persistent cookies [In reply to] Can't Post

From the docs:

raw_cookie()

Returns the HTTP_COOKIE variable, an HTTP extension implemented by Netscape browsers version 1.1 and higher, and all versions of Internet Explorer. Cookies have a special format, and this method call just returns the raw form (?cookie dough). See cookie() for ways of setting and retrieving cooked cookies.

Called with no parameters, raw_cookie() returns the packed cookie structure. You can separate it into individual cookies by splitting on the character sequence "; ". Called with the name of a cookie, retrieves the unescaped form of the cookie. You can use the regular cookie() method to get the names, or use the raw_fetch() method from the CGI::Cookie module.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives