SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parse POST array into individual variables

    Rather than 20 or so statements
    $var1 = $_POST'var1'];
    I'd like to parse the POST array into individual variables. Can anyone give me a start please?
    blessed are the cracked for they let in the light

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure, something like this should do it:-
    PHP Code:
    <?php
    //--> a white list to enusre we dont bring everything sent into scope
    $aAllowed = array('name','password','age');
    //--> iterate through all items
    foreach ($aAllowed as $sAllowedItem){
        
    //--> assing the variable
        
    ${$sAllowedItem} = $_POST$sAllowedItem ];
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Location
    sudo rm -rf /
    Posts
    386
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you can turn on register_globals but this is not recommended.

  4. #4
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No, don't turn register globals on. It's a big security risk if you don't handle the variables properly.

    You could use extract($_POST); but that doesn't contain the extra whitelist check that SilverBulletUK's code has.

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    No, don't turn register globals on. It's a big security risk if you don't handle the variables properly.
    I could almost hear the panic in your voice Stormrider!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys. No I wouldn't think of turning on register_globals for several reasons...

    Much obliged, chaps.
    blessed are the cracked for they let in the light


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •