Page 1 of 1
Getting any variables passed in a url string
Posted: Tue Dec 21, 2010 1:08 pm
by gotornot
Hi
I am trying to get all variables passed in a url string and wondered how would i do it?
I dont know what they are yet and i need to set key as value.
Any help?
Re: Getting any variables passed in a url string
Posted: Tue Dec 21, 2010 2:06 pm
by s992
I'm not sure I fully understand your question, but this will work to loop through all of your $_GET data and assign it to a variable with the same name as they key:
Code: Select all
foreach($_GET as $key => $value) {
$$key = $value;
}
If you have an URL string like
index.php?value1=something&value2=somethingelse&value3=anotherthing, this will set $value1 = "something", $value2 = "somethingelse", and $value3 = "anotherthing".
Without knowing the names of the values you are getting, though, I'm not sure how this will be of much use.
Re: Getting any variables passed in a url string
Posted: Tue Dec 21, 2010 2:21 pm
by Jonah Bron
s992 wrote:but this will work to loop through all of your $_GET data and assign it to a variable with the same name as they key:
extract() will do that, but it's not a good idea.
gotornot wrote:... I dont know what they are yet and i need to set key as value.
What do you mean? Didn't you make the form?
Re: Getting any variables passed in a url string
Posted: Tue Dec 21, 2010 2:35 pm
by s992
Jonah Bron wrote:extract() will do that, but it's not a good idea.
Wow, that's a whole lot easier!

Re: Getting any variables passed in a url string
Posted: Tue Dec 21, 2010 2:44 pm
by requinix
s992 wrote:Jonah Bron wrote:extract() will do that, but it's not a good idea.
Wow, that's a whole lot easier!

And a whole lot less safe too!
$_GET is an array with everything in the URL. Use that.
Re: Getting any variables passed in a url string
Posted: Wed Dec 22, 2010 11:00 am
by s992
What's the difference between using my method and extract()?
Re: Getting any variables passed in a url string
Posted: Wed Dec 22, 2010 12:08 pm
by requinix
s992 wrote:What's the difference between using my method and extract()?
Nothing. Same effect, but using extract() is shorter and faster.
Re: Getting any variables passed in a url string
Posted: Wed Dec 22, 2010 12:34 pm
by Jonah Bron
The only way extract() would be remotely safe is if you use the EXTR_SKIP and EXTR_PREFIX_ALL flags. Even then, any values that are not set that you're expecting will not be there, and you'll get an error. This might be the safest route while using extract:
Code: Select all
extract($_GET, EXTR_SKIP|EXTR_PREFIX_ALL, 'get');
if (!isset($get_somevalue, $get_someothervalue, /* ... */)) {
// error
}
Re: Getting any variables passed in a url string
Posted: Wed Dec 22, 2010 12:44 pm
by s992
tasairis wrote:s992 wrote:What's the difference between using my method and extract()?
Nothing. Same effect, but using extract() is shorter and faster.
Thanks - your post gave me the impression that somehow my method was safer than using extract(), but I can see now what you meant.
Anyway - sorry to derail this thread.
