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
Maybe you need something like this: <?php foreach($_GET as $key => $value){ $$key = $value; } echo($tree); ?> PHP:
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:
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.