Jump to content

$_POST and foreach loop question


dk4210

Recommended Posts

Hello Guys,

 

I have a question. 

 

I have the following code to grab all of my $_POST vars and run through my filter function

 

foreach($_POST as $key => $value) {
    $mydata[$key] = filter($value);
     }

 

Before using the foreach loop I was just doing the basic

 

$var1 = $_POST['var1'];

$var2 = $_POST['var2'];

 

I'm going to have about 40 - 50 $_POST vars so my question is how do i return it as just a $var name instead of having to do this

 

echo "<br><br><br>example1" . $mydata['var1'];

echo "<br><br><br>example2" . $mydata['var2'];

 

I guess its hard for me to explain..

 

Thanks for your help!

 

 

Link to comment
Share on other sites

You can use array_map() instead of your foreach() loop (which will leave the filtered results in the $_POST array). It will be 2-3 times faster and won't double the about of memory needed to store the data in memory.

 

Why do you want to make individual variables? The $_POST variables are perfectly fine variables.

 

If you still want to use individual variables, you should only convert expected variables (so that hackers posting data to your code cannot inject their variables into your code, possibly bypassing security on your site and taking it over) or you should insure that the variables you create have their own unique name-space so that they cannot overwrite any of your existing program variables. You can use extract() to do this on the original $_POST array or your intermediate $mydata array, but please use the EXTR_PREFIX_ALL parameter so that the created variables cannot overwrite any of your existing program variables.

 

Link to comment
Share on other sites

Thanks for the quick reply. The reason I want to have individual vars is because when I found out about the foreach loop already had many vars that my function depends on.

 

For example I have the following function

//Add a flood control function to check to see if the title,body & member id match
Flood_Control($ad_title,$member_id,$ad_body); 

 

Notice that I have the vars in there.. I hate to recode all that to  this

 

Flood_Control( $mydata['ad_title'], $mydata['memberid'], $mydata['ad_body']); 

 

Seems to defeat the purpose..

 

If I had a way that I could reference to a $var as opposed to $mydata['$var'] would be great..

 

Can you please give me an example of the array_map();?

 

If you still want to use individual variables, you should only convert expected variables

 

Please explain more on that.

 

I am going to research this ---> EXTR_PREFIX_ALL

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.