开发者

Importing post data into an array

开发者 https://www.devze.com 2023-02-05 11:14 出处:网络
I have a page \"bottom1.php\" it has 16 select menus, labled rating1-rating16. When the user submits this form, the next page has a script that looks like this:

I have a page "bottom1.php" it has 16 select menus, labled rating1-rating16. When the user submits this form, the next page has a script that looks like this:

//pulling data from post into array.
import_request_variables("p", "form_"); 
$scores= array($form_rating1,$form_rating2,$form_rating3,$form_rating4,$form_rating5,$form_rating6,$form_rating7,$form_rating8,$form_rating9,$form_rating10,$form_rating11,$form_rating12,$form_rating13,$form_rating14,$form_rating15,$form_rating16);
//putting array into session
$_SESSION['scores']=$scores;

the user is then directed to another page where an additional 16 selections are made, then the same script is ran, onl开发者_如何学编程y this time the array is stored into the session as "scores2".

The user is shown the output from each array, and then when the user confirms that the values are correct, each member of the scores, and scores2 array is parsed out, put into another variable, and then inserted into a database.

I know this is a primitive way of accomplishing this, but its the only way I know how.

This method worked with php configuration of 5.2.13, but I switched to a server with a configuration of 5.1.6 and now this script wont work. This script is critical to my site. Thanks for your help!

The parsing script looks like this:

$fpage=$_SESSION['scores'];
$spage=$_SESSION['scores2'];

$score1 = $fpage['0'];
$score2 = $fpage['1'];
$score3 = $fpage['2'];
$score4 = $fpage['3'];
$score5 = $fpage['4'];
...
$score31 =$spage['13'];
$score32 =$spage['14'];
$score33 =$spage['15'];

And then I insert $score1 - $score33 into my db..


The way you are doing it isn't exactly safe, for the same reason register_globals() is/was a bad idea. If you happen to prefix one of your internal variables with 'form_', the user could overwrite that value and potentially inject harmful code (depending on how that variable is used).

A better way to do this would be to filter the array directly into a new array using array_filter() or preg_grep().

Oliver A.'s solution only works in the scenario that all of the array keys will be in the form of 'form_rating', but your original script assumed a prefix of 'form_'.

preg_grep could be used as follows:

$keys = preg_grep('/^form_/', array_keys($_POST));
$scores = array();
foreach ($keys as $key) {
  $scores[$key] = $_POST[$key];
}


I do not have a php enviroment to test this right now but this should work:

  $scores = array();
    for($i=1;array_key_exists("form_rating{$i}",$_POST);$i++){
      $scores[] = $_POST["form_rating{$i}"];
    }

EDIT: I tested and working


If the script does not work when PHP changes version then:

  1. Check the logs to see the errors
  2. Check the PHP Changelog for any function or control structure change that my halt your script.

Just an issue that it happen to me a lot when switching between 5.1 and 5.2 is the Elvis operator ?: which is not recognized in php 5.1.

This is why is important to develop on the same kind of configuration that the server has, or to implement unit-testing and automatic deployments which can detect this problems before reaching production.

0

精彩评论

暂无评论...
验证码 换一张
取 消