WarrenBullockIII has asked for the wisdom of the Perl Monks concerning the following question:

The following script is just a rough test script I am trying to use to experiment with cookies. The script checks for the existance of a cookie first. If it finds an existing cookie it prints out the a welcome message. If it doesn't find a cookie it prints the form to allow the person to send themselves a cookie. When they visit the page again it will automatically display the welcome message. The problem I am having with this script is that I would like the welcome message to say something like: Welcome back: John instead I get somthing like: Welcome back: user=John I am not sure how to display the cookie value without the name... Any help would be appreciated.
#!/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"; }

Replies are listed 'Best First'.
Re: Pulling out the value from a cookie
by dws (Chancellor) on Jul 25, 2002 at 02:41 UTC
    instead I get somthing like: Welcome back: user=John I am not sure how to display the cookie value without the name ...

    If you're going to start futzing with cookies at a low-level (instead of, say, using CGI::Cookie), it helps to understand what cookies are and how they're constructed and transfered.

    RFC 2109 is a good starting point, though it's a bit formal. You might find Netscape Cookies to be more readable.

    Once you've had a go at one or both of these, give the CGI::Cookie POD a read.

Re: Pulling out the value from a cookie
by grep (Monsignor) on Jul 25, 2002 at 02:52 UTC

    Straight from the POD of CGI.pm

    To create a cookie
    $cookie = $query->cookie(-name=>'sessionID', -value=>'xyzzy', -expires=>'+1h', -path=>'/cgi-bin/database', -domain=>'.capricorn.org', -secure=>1); print $query->header(-cookie=>$cookie);


    To retrieve a cookie, request it by name by calling cookie() method without the -value parameter
    use CGI; $query = new CGI; $riddle = $query->cookie('riddle_name'); %answers = $query->cookie('answers');


    grep
    Just me, the boy and these two monks, no questions asked.