Category: | CGI Programming |
Author/Contact Info | Daniel Cormier, dcormier@themaker.net |
Description: | I don't use CGI.pm execpt to pull the params from the users request. I prefer to wrote my own raw HTML, so I never use that feture of CGI.pm. I'm sure there are more of you out there who do the same thing, so I'm posting what I use to pull params from a request. It pulls the users cookie, form (for either get or post) and puts them into 3 hashes: %cookie, %form and %params. %params contains %form and %cookie. If there's a param with the same name in the form and cookie, it's over-written by the form, but still accessable through %cookie. I've done a good amount of testing and it seems fool proof, so you just know someone will be making a better fool. Enjoy! |
sub get_params { my @cookie_data = split(/; /, $ENV{'HTTP_COOKIE'}); foreach $cookie (@cookie_data){ (my $one, my $two) = split(/=/, $cookie); $cookies{$one} = $two; $params{$one} = $two; } if ($ENV{'REQUEST_METHOD'} eq "GET"){ my @form_data = split(/&/, $ENV{'QUERY_STRING'}); foreach $form (@form_data){ (my $one, my $two) = split(/=/, $form); if ($one =~ m/\+/){ $one =~ s/(\+)/" "/eg; } if ($two =~ m/\+/){ $two =~ s/(\+)/" "/eg; } $one =~ s/%([\da-fA-F][\dA-Fa-f])/pack("C", hex($1))/eg; $two =~ s/%([\da-fA-F][\dA-Fa-f])/pack("C", hex($1))/eg; $form{$one} = $two; $params{$one} = $two; } } if ($ENV{'REQUEST_METHOD'} eq "POST"){ read(STDIN, my $form_data, $ENV{'CONTENT_LENGTH'}); my @form_data = split(/&/, $form_data); foreach $form (@form_data){ (my $one, my $two) = split(/=/, $form); if ($one =~ m/\+/){ $one =~ s/(\+)/" "/eg; } if ($two =~ m/\+/){ $two =~ s/(\+)/" "/eg; } $one =~ s/%([\da-fA-F][\dA-Fa-f])/pack("C", hex($1))/eg; $two =~ s/%([\da-fA-F][\dA-Fa-f])/pack("C", hex($1))/eg; $form{$one} = $two; $params{$one} = $two; } } return %cookie, %form, %params; } |
Back to
Code Catacombs