$_get

Discussion in 'PHP' started by Fracisc, Nov 29, 2008.

  1. #1
    I remember I used a command to get all the variables from an URL. I don`t want to use separate $_GET for every single variable. Is there anyone who knows how can I get all the variable values with one command?
     
    Fracisc, Nov 29, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    print_r($_GET) will display all variables

    and if you like to loop the array use

    foreach ($_GET AS $key => $value)
    {
    echo $key . ' has the value ' . value . '<br />';
    }
     
    EricBruggema, Nov 29, 2008 IP
  3. peeeev

    peeeev Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you may want also use
    extract($_GET);
    PHP:
     
    peeeev, Nov 29, 2008 IP
  4. jspash

    jspash Active Member

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #4
    I've never heard of extract but it sounds pretty cool. I wonder what the security implications are, if any? Recently I've turned off REGISTER_GLOBALS because of possible problems. extract feels a little like it might have the same problems.
     
    jspash, Nov 29, 2008 IP
  5. peeeev

    peeeev Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Actually there is no security implications, if you use the second and third parameter:

    extract($_GET, EXTR_PREFIX_ALL, 'safe_get');
    PHP:
    this way you will access your variables in the query
    http://somwdomain.com/?id=5&action=view
    like
    $safe_get_id , $safe_get_action
     
    peeeev, Nov 29, 2008 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    I've learned not to screw around with any of that stuff both for security and efficiency later on in the code.

    I create an array of variables I expect to work with in the beginning like so.

    $svars = array(
       'one' => '',
       'two' => ''.
       'three' => ''
    );
    Code (markup):
    After that, I can loop through the array to gather information I want to use. I can conditionally set further variables which I check for using "isset" in other areas. I can unset certain indexes and pass the array to my PDO prepared statements. I can remove certain indexes and pass the array to my template class so that collected values can be re-inserted into the HTML form.

    It may seem like a lot of extra work at first, but once you get used to writing entire applications using newer PHP features you start to see how much less work it actually is.
    I'll probably look into using a "request" namespace instead of an array once PHP6 calms down a little.
     
    joebert, Nov 29, 2008 IP
  7. peeeev

    peeeev Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I agree, that is great method.

    I used it when it came to have ability to have the "back" button while submitting POST form. It works great. But it is really too much pain to edit all these arrays when some variable is added or deleted in the form.
     
    peeeev, Nov 29, 2008 IP
  8. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #8
    Then you're doing it wrong, plain and simple. :)
     
    joebert, Nov 29, 2008 IP