Jump to content

Archived

This topic is now archived and is closed to further replies.

extrovertive

Using array_map for incoming form data?

Recommended Posts

I noticed some ppl use:

[code=php:0]
$cu_s_number = mysql_real_escape_string($_POST['cu_s_number']);
$cu_s_sample = mysql_real_escape_string($_POST['cu_s_sample']);
$cu_s_wt = mysql_real_escape_string($_POST['cu_s_wt']);
$cu_s_tare = mysql_real_escape_string($_POST['cu_s_tare']);
$cu_s_poste = mysql_real_escape_string($_POST['cu_s_post']);
$cu_s_diff_value = mysql_real_escape_string($_POST['cu_s_diff_value']);
[/code]

However, within a form, if I would like to escape all the data, is this more efficient or is there a problem with this version below?

[code=php:0]

if(isset($_POST['submit']))
{
array_pop($_POST); //remove the submit variable
$_POST = array_map("mysql_real_escape_string", $_POST);

  foreach($_POST as $variable=>$value)
  {
    $$variable = $value;
  }

}

[/code]

Share this post


Link to post
Share on other sites
I don't see a problem with your second script, but if you are looping through the post array anyway, why not just escape it there? Less typing involved...
[code]<?php
if(isset($_POST['submit'])) {
    array_pop($_POST); //remove the submit variable
    foreach($_POST as $variable=>$value) {
        $$variable = mysql_real_escape_string($value);
    }
}
?>[/code]

Share this post


Link to post
Share on other sites
Why remove the submit variable?!

Why remove anything from $_POST in fact?

Also, extracting variables frmo user input is not a wise idea. This is why regsiter_globals is frowned upon.

It is best practice to explicitly use the data you require, $_POST can contain as many fields as the user wishes. You will also have problems if the user submits an array within $_POST if you use that snippet.

Share this post


Link to post
Share on other sites

×

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.