1

I have built an empty associative array whos keynames refer to submitted post data. I can capture the postdata just fine, but I run into trouble trying to instantiate variables who's names match the array key.

for example:

$insArray = array('rUsername'=>'', 'rPass'=>'', 'rQuestion'=>'', 'rAnswer'=>'', 'rFName'=>'', 'rLName'=>'', 'rBDateD'=>'', 'rBDateM'=>'', 'rBDateY'=>'', 'rHCheck'=>'', 'rHCeckOption'=>'', 'rEmail'=>'');

foreach($insArray as $key=>$value){
    if (filter_input(INPUT_POST, $key) != ''){
        $key = stripslashes(filter_input(INPUT_POST, $key)); 
        $insArray[$key] = $key;
    }       
}

First line creates the empty array, then the foreach loops through this array. Now it gets tricky.

filter_input(INPUT_POST, $key) captures the value located in the post data matching the current key, rUsername in this case

$key is where the problem lies. I want the NAME of the new variable to be the associative key name, for example I want to replace $key with $rUsername in the first iteration, $rPass in the second, and so on. I tried using two $$, but I know that's not right. Never tried doing this before, but it would be helpful if I could figure it out.

UPDATE:

This is the final code which was a combination of two of the answers provided.

if (isset($_POST['submit'])) {
    //Build array of variables to be put into database
    $insArray = array('rUsername'=>'', 'rPassword'=>'', 'rQuestion'=>'', 'rAnswer'=>'', 'rFName'=>'', 'rLName'=>'', 'rBDateD'=>'', 'rBDateM'=>'', 'rBDateY'=>'', 'rHCheck'=>'', 'rHCheckOption'=>'', 'rEmail'=>'');

    foreach(array_keys($insArray) as $key){
        $insArray[$key] = filter_input(INPUT_POST, $key);
        $$key = filter_input(INPUT_POST, $key);
    }
}

Gave me exactly the output I wanted, thanks guys!

1
  • so what is the error you are getting Commented May 6, 2012 at 4:40

4 Answers 4

2

You're not accessing $_POST at all, so all you're doing is taking some array members you defined yourself, filtering them for harmful POST characters (why would you attempt to inject your own code?) and then creating a new array from those self-defined key values.

If I'm guessing right at what you want, it should be this:

foreach(array_keys($insArray) as $key) {
    $insArray[$key] = stripslashes(filter_input(INPUT_POST, $_POST[$key]));
}

The use of stripslashes suggests that you're on a braindead version of PHP which has magic_quotes enable. You should upgrade to a modern version of PHP and/or turn them off.

Sign up to request clarification or add additional context in comments.

1 Comment

I am sadly at the mercy of my hosting provider which gives me an AMAZING deal for usage, thus I am tied to the php version they are using. As far as injecting my own code, I was setting the array keys for SOME of the fields submitted from a previous form submit. And instead of handcoding each grab of only the variables I wanted, I initialized an empty array with keynames matching the fieldnames I wanted for this array from the form. Built the array, and eventually pass that array into a handler function. That said, the array-keys answer was exactly what I was looking for. Thanks again!
1

The solution is change

$key = stripslashes(filter_input(INPUT_POST, $key));

to

$$key = stripslashes(filter_input(INPUT_POST, $key));

See http://www.php.net/manual/en/language.variables.variable.php

Also, recheck your code, which are doing some mistakes..

1 Comment

Variable variables! Another item I utilized in my code! Sadly, couldn't give two answers, I hate that part!
1

If I understand you correctly, Im going to suggest this approach:

$defaultValues = array('rUsername'=>'', 'rPass'=>'', 'rQuestion'=>'', 'rAnswer'=>'', 'rFName'=>'', 'rLName'=>'', 'rBDateD'=>'', 'rBDateM'=>'', 'rBDateY'=>'', 'rHCheck'=>'', 'rHCeckOption'=>'', 'rEmail'=>'');
$values = array_map('stripslashes', array_merge($defaultValues, array_filter($_POST)));
extract($values, EXTR_SKIP);
echo $rUsername;
echo $rPass;
.........

By using the snippet above, you have to take into account the following

  • Im using the extract function with EXTR_SKIP so you dont overwrite existing variables. Make sure to only use the variables you need in your code and sanitize them appropietly.

  • By using array_filter on the $_POST superglobal im "erasing" all empty or null variables. so if an expected key was not sent via $_POST, it defaults to the value specified by the $defaultValues array.

  • I dont quite understand why you are using filter_input without the third parameter (filter constants).

1 Comment

I was using the filter_input to grab the direct post stack to ensure nothing was added to the post after submit. Although the filter comments is an excellent suggestion!
0

Hope this will help, If not may be I have misunderstood the problem.

Instead of

$key = stripslashes(filter_input(INPUT_POST, $key)); 
$insArray[$key] = $key;

Try

$insArray[$key] =stripslashes(filter_input(INPUT_POST, $key));

Then after the foreach loop

extract($insArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.