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

Some people might consider that a feature.

But if you don't, and assuming you're using mod_perl 1.*, you might want to have a look at the "args" method. If that returns anything, you will have had parameters specified in the query string. And you could remove those arguments from what Apache::Request is returning to you.

Hope this helps.

Liz

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

Replies are listed 'Best First'.
Re: Re: Preventing GET and POST from getting mixed-up
by esh (Pilgrim) on Aug 19, 2003 at 08:34 UTC

    What if the same parameter names are specified both in the GET parameters and the POST parameters?

    Do you need to differentiate between these and know which values were passed in which place?

    -- Eric Hammond

      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

        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

      Do you need to differentiate between these and know which values were passed in which place?

      Ideally, yes.

      --
      TTFN, FNORD

      xaphod