best method to get values from $_POST and $_GET

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
lbyrd1984
Forum Newbie
Posts: 3
Joined: Tue Nov 13, 2007 7:51 am

best method to get values from $_POST and $_GET

Post by lbyrd1984 »

Hi,
I am converting some legacy php scripts for use under php5 and I do not want to rewrite them at this time. They were written under php3 with register_globals set to on. Now I need to pass the variables and their values from the forms to the next script. I am currently using the following code in each script to get the form parameters to local variables. The question I have is is this the best method, what better methods would you suggest and are there any dangers that you see using this method? Thanks in advance for any suggestions.

The code put at that top of each script that needs to capture data from the previous form:


Code: Select all

$my_vars = $_POST;
foreach ($my_vars as $my_key => $my_value ) {
    $$my_key = $my_value;
}
$my_vars = $_GET;
foreach ($my_vars as $my_key => $my_value ) {
    $$my_key = $my_value;
}
 
Thanks
Last edited by lbyrd1984 on Fri Feb 01, 2008 12:16 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: best method to get values from $_POST and $_GET

Post by Christopher »

That is exactly what the $_REQUEST superglobal is. Better would be to encapsulate the values you want, plus related information about the request (Post? Ajax?) in a object perhaps.
(#10850)
lbyrd1984
Forum Newbie
Posts: 3
Joined: Tue Nov 13, 2007 7:51 am

Re: best method to get values from $_POST and $_GET

Post by lbyrd1984 »

Thanks arborint for the reply. So I could just use $_REQUEST in one foreach loop vice having two (to keep in my simple example), correct? Could you point me to information about using encapsulation and an object to get this same information please? Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: best method to get values from $_POST and $_GET

Post by Christopher »

Why copy $_REQUEST to a bunch of variables (which you would do with extract($_REQUEST)) ? Just use $_REQUEST.
(#10850)
lbyrd1984
Forum Newbie
Posts: 3
Joined: Tue Nov 13, 2007 7:51 am

Re: best method to get values from $_POST and $_GET

Post by lbyrd1984 »

Thanks for your comments,
The short answer is laziness and time constraints. The code uses the local variables all over the place and it seemed easier at the time to just get the passed variables into local variables. I am trying to get the code converted to not use register_globals on as we have been doing in the past and to switch to a new server so we can retire the old one.
I have been away from php for a little bit until this needed to be accomplished before we switched.

Thanks for all the help. I am still interested in your thoughts on using an object to replace this function if you can point me in a direction.

Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: best method to get values from $_POST and $_GET

Post by Chris Corbyn »

Hmm, and unfortunately a search/replace over the codebase won't clean things up neither... I agree that using extract($_REQUEST) is probably the best way to emulate register_globals, although the CHANGES file for PHP 6 (which drops register_globals completely) does some crazy voodoo magic to make it work.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: best method to get values from $_POST and $_GET

Post by thinsoldier »

lbyrd1984 wrote:The short answer is laziness and time constraints. The code uses the local variables all over the place and it seemed easier at the time to just get the passed variables into local variables.
Try using an editor with features that help with refactoring. Using the new zend/eclipse+pdt thing I can right click on $myvar, choose 'refactor', 'rename' and change it to $_POST['myvar'] and at least 90% of the places I needed the change to happen will happen. Might not be the solution for you since you're probably working with a lot more code than I am but for me it's great.
Warning: I have no idea what I'm talking about.
Post Reply