Getting any variables passed in a url string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gotornot
Forum Commoner
Posts: 54
Joined: Fri Jul 31, 2009 2:30 am

Getting any variables passed in a url string

Post 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?
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Getting any variables passed in a url string

Post 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.
User avatar
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

Post 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?
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Getting any variables passed in a url string

Post by s992 »

Jonah Bron wrote:extract() will do that, but it's not a good idea.
Wow, that's a whole lot easier! :D
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Getting any variables passed in a url string

Post 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! :D
And a whole lot less safe too!

$_GET is an array with everything in the URL. Use that.
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Getting any variables passed in a url string

Post by s992 »

What's the difference between using my method and extract()?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Getting any variables passed in a url string

Post by requinix »

s992 wrote:What's the difference between using my method and extract()?
Nothing. Same effect, but using extract() is shorter and faster.
User avatar
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

Post 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
}
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Getting any variables passed in a url string

Post 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. :wink:
Post Reply