开发者

I need a more efficient way of checking if multiple $_POST parameters isset

I have these variables, and I need to check if all of them isset(). I feel there has to be a more efficient way of checking them rather than one at a time.

$jdmMethod = $_POST['jdmMethod'];
$cmdMethod = $_POST['cmdMethod'];
$vbsMethod = $_POST['vbsMethod'];
$blankPage 开发者_如何学C= $_POST['blankPage'];
$facebook = $_POST['facebook'];
$tinychat = $_POST['tinychat'];
$runescape = $_POST['runescape'];
$fileUrl = escapeshellcmd($_POST['fileUrl']);
$redirectUrl = escapeshellcmd($_POST['redirectUrl']);
$fileName = escapeshellcmd($_POST['fileName']);
$appData = $_POST['appData'];
$tempData = $_POST['tempData'];
$userProfile = $_POST['userProfile'];
$userName = $_POST['userName'];


Try this

     $allOk = true;
     $checkVars = array('param', 'param2', …);
     foreach($checkVars as $checkVar) {
          if(!isset($_POST[$checkVar]) OR !$_POST[$checkVar]) {
               $allOk = false;
               // break; // if you wish to break the loop
          }
     }

     if(!$allOk) {
         // error handling here
     }


I like to use a function like this:

// $k is the key
// $d is a default value if it's not set
// $filter is a call back function name for filtering
function check_post($k, $d = false, $filter = false){
    $v = array_key_exists($_POST[$k]) ? $_POST[$k] : $d;
    return $filter !== false ? call_user_func($filter,$v) : $v;
}

$keys = array("jdmMethod", array("fileUrl", "escapeshellcmd"));
$values = array();
foreach($keys as $k){
    if(is_array($k)){
        $values[$k[0]] = check_post($k[0],false,$k[1]);
    }else{
        $values[$k] = check_post($k[0]);
    }
}

You could extend the keys array to contain a different default value for each post-value if you wish.

EDIT:

If you want to make sure all of these have a non-default value you could do something like:

if(sizeof(array_filter($values)) == sizeof($keys)){
    // Not all of the values are set
}


Something like this:

$jdmMethod = isset($_POST['jdmMethod']) ? $_POST['jdmMethod'] : NULL;

It's Ternary Operator.


I think this should work (not tested, from memory)

function handleEmpty($a, $b) {
   if ($b === null) {
       return false;
   } else {
      return true;
}

array_reduce($_POST, "handleEmpty");


Not really. You could make a list of expected fields:

$expected = array(
 'jdmMethod',
 'cmdMethod',
 'fileName'
); // etc...

... then loop those and make sure all the keys are in place.

$valid = true;
foreach ($expected as $ex) {
 if (!array_key_exists($ex, $_POST)) {
   $valid  = false;
   break;
 }
 $_POST[$ex] = sanitize($_POST[$ex]);
}

if (!$valid) {
  // handle the problem
}

If you can develop a generic sanitize function, that will help - you can just sanitize each as you loop.

Another thing I like to use is function that gives a default as it sanitizes.

function checkParam($key = false, $default = null, $type = false) {
    if ($key === false)
        return $default;

    $found_option = null;           
    if (array_key_exists($key,$_REQUEST))
        $found_option = $_REQUEST[$key];
    if (is_null($found_option))
        $found_option = $default;       
    if ($type !== false) {
        if ($type == 'string' && !is_string($found_option))
            return $default;
        if ($type == 'numeric' && !is_numeric($found_option))
            return $default;
        if ($type == 'object' && !is_object($found_option))
            return $default;
        if ($type == 'array' && !is_array($found_option))
            return $default;
    }
    return sanitize($found_option);
}

When a default is possible, you'd not want to do a loop, but rather check for each independently:

$facebook = checkParam('facebook', 'no-facebook', 'string);


It is not the answer you are looking for, but no.

You can create an array an loop through that array to check for a value, but it doesn't get any better than that.

Example:

$postValues = array("appData","tempData",... etc);
foreach($postedValues as $postedValue){
   if(isset($_POST[$postedValue])){
      ...
   }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜