1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Obtaining all vars from url

Discussion in 'PHP' started by rjbathgate, Aug 25, 2010.

  1. #1
    Hi,

    I have a url containing a whole heap of variables. eg:

    index.php?a=1&b=2&tree=green etc etc

    On the page I can then get each one and turn it into a usable variable using:

    $tree = $_GET['tree'];

    However, is there an easier/automatic way to get all from the url and automatically populate variables?

    basically I have a page which when submitted it can send a whole range of different variables - to many for me to hardcode define using $_GET, but I need those variables as parsed via the URL to be accessible as new variables on the page.

    Hope that makes some sort of sense.

    Thinking something within a <?php foreach(@$_REQUEST as $key => $value): ?> would do the trick, just not sure what...

    Thanks in advance
    Rob
     
    rjbathgate, Aug 25, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Maybe you need something like this:

    
    <?php
    foreach($_GET as $key => $value){
        $$key = $value;
    }
    
    echo($tree);
    ?>
    
    PHP:
     
    s_ruben, Aug 25, 2010 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Use parse_str, which will basically do what s_ruben showed and more (in the event you pass an array in the URL as well):

    
    $str = $_SERVER['QUERY_STRING'];
    // Example: $str = "first=value&arr[]=foo+bar&arr[]=baz";
    
    parse_str($str);
    echo $first;  // value
    echo $arr[0]; // foo bar
    echo $arr[1]; // baz
    
    // Another way to do it:
    parse_str($str, $output);
    
    echo $output['first'];  // value
    echo $output['arr'][0]; // foo bar
    echo $output['arr'][1]; // baz
    
    PHP:
     
    ThePHPMaster, Aug 26, 2010 IP
  4. rjbathgate

    rjbathgate Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Spot on, many thanks!
     
    rjbathgate, Aug 26, 2010 IP
  5. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #5
    the other option would be using a register globals on emulator to simulate register globals on but then raises more security problems. However using it the way shown above things still have to be sanitized before executing the values anyways.

    by using a emulator for register globals on you can simply just call it as $tree rather then grabbing the $_GET or $_POST values.
     
    lowridertj, Aug 26, 2010 IP