The other day I posted a message asking for assitance on cookies the message was this: 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"; }
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($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"; }

In reply to Pulling out the value of a cookie: Revisited by WarrenBullockIII

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.