Page 1 of 1

best method to get values from $_POST and $_GET

Posted: Fri Feb 01, 2008 9:09 am
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

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

Posted: Fri Feb 01, 2008 9:51 am
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.

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

Posted: Fri Feb 01, 2008 9:59 am
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

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

Posted: Fri Feb 01, 2008 12:07 pm
by Christopher
Why copy $_REQUEST to a bunch of variables (which you would do with extract($_REQUEST)) ? Just use $_REQUEST.

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

Posted: Fri Feb 01, 2008 12:14 pm
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

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

Posted: Fri Feb 01, 2008 10:11 pm
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.

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

Posted: Sun Feb 10, 2008 1:08 am
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.