in reply to Re: Re: Preventing GET and POST from getting mixed-up
in thread Preventing GET and POST from getting mixed-up

I'm afraid so.

I'd appreciate it if someone would know a better way, as I will soon have to deal with this myself as well.

Liz

  • Comment on Re: Re: Re: Preventing GET and POST from getting mixed-up

Replies are listed 'Best First'.
Re: Re: Re: Re: Preventing GET and POST from getting mixed-up
by xaphod (Monk) on Aug 21, 2003 at 09:31 UTC

    I'd appreciate it if someone would know a better way

    Well, it seems like I've had to do it the way I thought I'd have to do it. So here is my code, make of it what you will (feedback welcome). One caveat... all this code is copied out of an object which is currently too messy to post. In order to work you'll need to have $self->{'request'} = Apache->request; somewhere in your initialisation code.

    # @foo = get_param() # returns the parameter names (or undef if none) # $foo = get_param("param") # returns the named parameter (or undef if not) # @foo = get_param("param") # as above in list context # ================================================== sub get_param { my $self = shift; my $param = shift || undef; unless (exists $self->{get_vars}) { # Set to undef if no args unless ($self->{'request'}->args) { warn "I see no GET vars!\n" if $DEBUG; return $self->{'get_vars'} = undef; } # Now run the query_string through the parser $self->_parse('get_vars', scalar($self->{'request'}->args) +); } return undef unless defined $self->{'get_vars'}; return keys %{$self->{'get_vars'}} unless $param; return undef unless exists($self->{'get_vars'}->{$param}); return $self->{'get_vars'}->{$param} unless ref $self->{'get_vars' +}->{$param}; return wantarray ? @{ $self->{'get_vars'}->{$param} } : "@{ $self- +>{'get_vars'}->{$param} }"; return undef; ## Inconceivable! } # ================================================== sub post_param { my $self = shift; my $param = shift || undef; unless (exists $self->{'post_vars'}) { # Set to undef if not POST unless ($self->{'request'}->method_number == M_POST) { warn "I see no POST vars\n" if $DEBUG; return $self->{'post_vars'} = undef; } # Now run the query_string through the parser $self->_parse('post_vars', scalar($self->{'request'}->cont +ent)); } return undef unless defined $self->{'post_vars'}; return keys %{$self->{'post_vars'}} unless $param; return undef unless exists($self->{'post_vars'}->{$param}); return $self->{'post_vars'}->{$param} unless ref $self->{'post_var +s'}->{$param}; return wantarray ? @{ $self->{'post_vars'}->{$param} } : "@{ $self +->{'post_vars'}->{$param} }"; return undef; ## Inconceivable! } # ================================================== sub _parse { my $self = shift; my $dst = shift; my $str = shift; warn "_parse: $dst $str\n" if $DEBUG; foreach my $pair (split /&/, $str) { $pair =~ tr/+/ /; $pair =~ s/%([\da-f][\da-f])/chr( hex($1) )/egi; my( $name, $value ) = split /=/, $pair; $value = $value || ""; if ( exists ($self->{$dst}->{$name}) ) { if ( ref $self->{$dst}->{$name} ) { push @{ $self->{$dst}->{$name} }, $value; } else { $self->{$dst}->{$name} = [ $self->{$dst}->{$name}, $va +lue ]; } } else { $self->{$dst}->{$name} = $value; } } }
    --
    TTFN, FNORD

    xaphod