Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all submitted form values in PHP and automatically assign them to variables?

Tags:

post

forms

php

get

I'm trying to migrate a website from one host to another. On the first host, when you submit a form, all of the form values are automatically stuck into variables with the input name (this is PHP). On the new host, these values are all null unless I do this:

$data = $_GET['data'];

Is there a PHP configuration setting that is causing this? If there isn't, is there an easy way to loop through all of the $_GET variables and automatically assign their values to a variable with the same name?

Thanks!

like image 303
Dave Avatar asked Feb 04 '26 14:02

Dave


2 Answers

The setting is register_globals, but it is now deprecated and strongly advised against using it because it is a security risk. Anyone can set variables in your script which might interact in a negative or unexpected way with your code.

If you absolutely must, you can do it like this:

foreach ($_GET as $key=>$value) {
    $$key = $value;
}

or, more simply:

import_request_variables("g");

or, to make it a little safer:

import_request_variables("g", "myprefix_"); // This way forces you to use "myprefix_" 
// in front of the variables, better ensuring you are not unaware 
// of the fact that this can come from a user

extract($_GET) could also work, as someone else pointed out, and it also allows specification (via extra arguments) of adding a prefix or what to do if your extraction conflicts with an already existing variable (e.g., if you extracted after you defined some other variables).

like image 186
Brett Zamir Avatar answered Feb 06 '26 03:02

Brett Zamir


Look at the extract function : http://www.php.net/manual/en/function.extract.php

like image 23
Yoann Avatar answered Feb 06 '26 04:02

Yoann