Getting any variables passed in a url string
Moderator: General Moderators
Getting any variables passed in a url string
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?
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
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:
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.
Code: Select all
foreach($_GET as $key => $value) {
$$key = $value;
}
Without knowing the names of the values you are getting, though, I'm not sure how this will be of much use.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Getting any variables passed in a url string
extract() will do that, but it's not a good idea.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:
What do you mean? Didn't you make the form?gotornot wrote:... I dont know what they are yet and i need to set key as value.
Re: Getting any variables passed in a url string
Wow, that's a whole lot easier!Jonah Bron wrote:extract() will do that, but it's not a good idea.
Re: Getting any variables passed in a url string
And a whole lot less safe too!s992 wrote:Wow, that's a whole lot easier!Jonah Bron wrote:extract() will do that, but it's not a good idea.
$_GET is an array with everything in the URL. Use that.
Re: Getting any variables passed in a url string
What's the difference between using my method and extract()?
Re: Getting any variables passed in a url string
Nothing. Same effect, but using extract() is shorter and faster.s992 wrote:What's the difference between using my method and extract()?
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Getting any variables passed in a url string
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
Thanks - your post gave me the impression that somehow my method was safer than using extract(), but I can see now what you meant.tasairis wrote:Nothing. Same effect, but using extract() is shorter and faster.s992 wrote:What's the difference between using my method and extract()?
Anyway - sorry to derail this thread.