Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re: Preventing GET and POST from getting mixed-up

by esh (Pilgrim)
on Aug 19, 2003 at 08:34 UTC ( [id://284820]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re: Re: Re: Preventing GET and POST from getting mixed-up
by liz (Monsignor) on Aug 19, 2003 at 08:41 UTC
    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
Re: Re: Re: Preventing GET and POST from getting mixed-up
by xaphod (Monk) on Aug 19, 2003 at 08:39 UTC

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

    Ideally, yes.

    --
    TTFN, FNORD

    xaphod

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-28 20:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found