Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Cookie Maker

by isotope (Deacon)
on Sep 09, 2000 at 01:51 UTC ( [id://31692]=sourcecode: print w/replies, xml ) Need Help??
Category: CGI Programming
Author/Contact Info isotope@skylab.org
Description: I use this CGI script every time I have to do some work with cookies. It can be used to set completely arbitrary cookies, and to renew, edit, or delete existing cookies. I wrote it a long time ago, so please excuse the newbie flavor to it. Maybe I'll work on an update soon. Consider it covered by the GPL.
#!/usr/local/bin/perl
$| = 1;
use CGI qw(:form);
use URI::Escape;
my $query = new CGI;

my $name = $query->param('name');
if ($name eq "") {
    form();
}
else {
    my $value = $query->param('value');
    my $exp = $query->param('exp');
    if ($query->param('cookie_action') eq "Delete") {
        $exp = "-1d";
    }
    my $path = $query->param('path');
    my $cookie = $query->cookie(-name=>$name,
                                -value=>$value,
                                -expires=>$exp,
                                -path=>$path);
    print $query->header(-cookie=>$cookie,
                         -expires=>'-1d',
                         -location=>'cookie.cgi');
    print <<"End";
    <HTML>
    <HEAD>
    <TITLE>Cookie maker</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF">
    <FONT SIZE="+2"><A HREF="cookie.cgi">Refresh</A></FONT><BR>
    </BODY>
    </HTML>
End
}

sub form {
    print $query->header(-expires=>'-1d');
    print <<"End";
    <HTML>
    <HEAD>
    <TITLE>Cookie Maker</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF">
    <H1>Please enter your cookie parameters</H1>
    <FORM ACTION="cookie.cgi" METHOD="GET">
    <INPUT TYPE="text" NAME="path" VALUE="/cgi-bin/"> Path<BR>
    <INPUT TYPE="text" NAME="name" VALUE=""> Cookie Name<BR>
    <INPUT TYPE="text" NAME="value" VALUE=""> Cookie Value<BR>
    <INPUT TYPE="text" NAME="exp" VALUE="+3M"> Expiration date<BR>
    <INPUT TYPE="submit" NAME="cookie_action" VALUE="Make cookie">
    </FORM>
    <BR>
End
    mycookies();
    print <<"End";
    </BODY>
    </HTML>
End
}

sub mycookies {
    print "Your cookies:\n";
    print "<TABLE BGCOLOR=\"#FFFF00\" BORDER=\"1\">\n";
    print "<TR><TH>Name</TH><TH>Value</TH><TH>Cookie Path</TH><TH>Rene
+wal Date</TH><TH>Action</TH></TR>\n";
    my @cookies = split(/\; /, $ENV{'HTTP_COOKIE'});
    foreach my $cookie (@cookies) {
        $cookie = uri_unescape($cookie);
        my ($name, $value) = split(/=/,$cookie);
        print "<TR>\n";
        print "<FORM ACTION=\"cookie.cgi\" METHOD=\"GET\">\n";
        print "<INPUT TYPE=\"hidden\" NAME=\"name\" VALUE=\"$name\">\n
+";
        print "<INPUT TYPE=\"hidden\" NAME=\"value\" VALUE=\"$value\">
+\n";
        print "<TD>$name</TD>\n";
        print "<TD>$value</TD>\n";
        print "<TD><INPUT TYPE=\"text\" NAME=\"path\" VALUE=\"/cgi-bin
+/\"></TD>\n";
        print "<TD><INPUT TYPE=\"text\" NAME=\"exp\" VALUE=\"+3M\"></T
+D>\n";
        print "<TD><INPUT TYPE=\"submit\" NAME=\"cookie_action\" VALUE
+=\"Renew\"><INPUT TYPE=\"submit\" NAME=\"cookie_action\" VALUE=\"Dele
+te\"></TD>\n";
        print "</FORM>\n";
        print "</TR>\n";
    }
    print "</TABLE>\n";
}
Replies are listed 'Best First'.
RE: Cookie Maker
by joe (Acolyte) on Oct 17, 2000 at 04:59 UTC
    Thanks isotope. very handy. One question though... How can you get the "path" and "expiration" from a cookie? Are they even sent down with a request? They don't show up even with CGI::raw_cookie.
      path and expiration are used internally by the browser. They are not transmitted to the server. Only the name/value pair for each cookie is actually sent to the server.

      --isotope
      http://www.skylab.org/~isotope/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://31692]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found