WarrenBullockIII has asked for the wisdom of the Perl Monks concerning the following question:
However, I had many people tell me that using Environment variables was quite dangerous and I still was stuck because I couldn't get the value alone from the cookie to display... I kept getting somthing like user=Warren instead of Warren. I modified my code a little and came up with this... Any comments are welcome :-)#!/usr/bin/perl use CGI; $query = new CGI; if($ENV{'HTTP_COOKIE'}){ print $query->header; print"<PRE>\n"; $cook = $ENV{'HTTP_COOKIE'}; @cookies = split /;/, $cook; foreach $new (@cookies){ print "Welcome back:" . $new; } print "</PRE>\n"; } else{ if($query->param('name')){ $cookie = $query->cookie(-name=>'user', -value=>$query->param('name'), -expires=>'+30d', -path=>'/'); print $query->header(-cookie=>$cookie); print "Thank you for registering!\n"; print "When you view this page again you will se a welcoming message i +nstead of the form<BR>"; } else{ print $query->header; print "<CENTER><H3>Testing a page with cookies</H3></CENTER>\n\n"; print "<P></P>\n\n"; #construct the form that asks for the username... print "<FORM METHOD=\"post\"\n>"; print "Please type your name: "; print "<INPUT TYPE=\"text\" NAME=\"name\" SIZE=20>\n"; print "<INPUT TYPE=\"submit\" VALUE=\"register\">\n"; print "</FORM>\n\n"; &end_page; } } sub start_page{ print "<HTML><HEAD><TITLE>Testing a script with cookies</TITLE></ +HEAD>\n\n"; print "<BODY>\n"; } sub end_page{ print "</BODY>\n\n"; print "</HTML>\n"; }
#!/usr/bin/perl use CGI; $query = new CGI; if($query->cookie('user')){ print $query->header; $content = $query->cookie('user'); @new = split ("=", $content); foreach $user (@new){ if($user !~ /^user$/){ print "Welcome back $user\n"; } }} else{ if($query->param('name')){ $cookie = $query->cookie(-name=>'user', -value=>$query->param('name'), -expires=>'+30d', -path=>'/'); print $query->header(-cookie=>$cookie); print "Thank you for registering!\n"; } else{ print $query->header; print "<CENTER><H3>Testing a page with cookies</H3> </CENTER> \n + \n"; print "<P></P>\n\n"; #construct the form that asks for the username... print "<FORM METHOD=\"post\"\n>"; print "Please type your name: "; print "<INPUT TYPE=\"text\" NAME=\"name\" SIZE=20>\n"; print "<INPUT TYPE=\"submit\" VALUE=\"register\">\n"; print "</FORM>\n\n"; &end_page; } } sub start_page{ print "<HTML><HEAD><TITLE>Testing a script with cookies</TITLE></ +HEAD>\n\n"; print "<BODY>\n"; } sub end_page{ print "</BODY>\n\n"; print "</HTML>\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pulling out the value of a cookie: Revisited
by dws (Chancellor) on Jul 25, 2002 at 17:35 UTC | |
|
Re: Pulling out the value of a cookie: Revisited
by BrowserUk (Patriarch) on Jul 25, 2002 at 17:57 UTC |