Is there any way top enumerate all PHP variables?

PHP $_POST has an Array structure.
I would like to run a script like the following

$postSize= … // how to get the POST variables number ?
for ($i=0; $i< $postSize; i++) {
echo $i . “–>” . … // how to get the POST variable with index = %i ?
}

What top level problem are you trying to solve?

The keys in the $_POST data are the form field names, which are strings.

You should not unconditionally loop over the $_POST data. You should have a definition of the expected field names and use this definition to access the submitted data.

I need to access variables without knowing their names.
I need to store POST Variables in an array containing the name and the value of any variable.

Data submitted to your site can come from anywhere, not just your forms, links, and cookies, can be set to anything, and cannot be trusted. Php even added a max_input_vars setting to try to protect against a massive amount of nefarious data being submitted.

If you are trying to make a reusable/configurable form processing script (this is a statement of a top level problem), your form will have a specific set of field names defined in it. You would use this same definition (as an array of the expected field names, their type, validation steps, and what processing they get used for) in the form processing code so that you only operate on expected fields.

You can simply use a foreach() loop to loop over all the names and values, but this will allow hackers to submit their data to your code and get it to do what they want, not what you expect.

1 Like

Thanks everyone for the advice,
The script needed to create a data passing area between two PHP programs where one must use what was entered from a FORM into the first.
Here below the script
The firsyt PHP Program
// Put the entire POST data as $_SESSION object
$_SESSION [“PostData”] = $_POST

The second PHP Program
//Retrieve the POST data fdom the $_SESSION
$postData=$_SESSION [“PostData”]

Now we can use $postData as an Array but also simulating that the second program was activated via HTTP POST.

This is useful if (and this is the case) you use object-oriented programming that maps HTTP POST. HTTP GET, records extracted from File or MYSQL into as many interacting objects.

I come to PHP from Java and, more precisely, from the J2EE architecture, so I am bound to extremely advanced object-oriented programming. I therefore created a PHP-INJECTOR, a tool that, from a porophile of any type, creates PHP Classes.

I therefore need to approach any WEB element in an “aseptic” programmatical way. .

My next step is to ricreate Java’s Enterprise JavaBeans template in PHP and I’m well underway…

Hello,
Thanks for this solution, this is very helpful for me.

Sponsor our Newsletter | Privacy Policy | Terms of Service