代码之家  ›  专栏  ›  技术社区  ›  Mankind1023

如何创建中心PHP输入验证文件并使用它?

  •  0
  • Mankind1023  · 技术社区  · 15 年前

    $rep = '';
    $clean_rep = '';
    $rep = $_GET[rep];
    
    //Basic Variable Validation
    switch ($rep){
        case 'All':
        case 'Ian':
        case 'Mike':
        case 'Stan':
        case 'Gena':
            $clean_rep = $rep;  
            break;
    }
    

    如果我把它放在一个单独的文件中,并包含在所需的页面中,我假设它必须在一个函数中,这样它才能被挑选出来:

    validation.php:
    function validateRep($rep){
        switch ($rep){
            case 'All':
            case 'Ian':
            case 'Mike':
            case 'Stan':
            case 'Gena':
                $return("Clean");
                break;
        }
    }
    

    一旦变量以“Clean”的形式返回,我是否将第一个脚本中的$rep赋给$Clean\u rep?还是有更好的方法?

    2 回复  |  直到 15 年前
        1
  •  1
  •   aularon    15 年前

    function validateRep($rep){
        return in_array($rep, array(
            'All', 'Ian', 'Mike', 'Stan', 'Gena', 
        ));
    }
    

    [in_array][1] 将检查第一个参数是否存在于第二个数组参数中,如果存在则返回true,如果不存在则返回false,然后返回 validateRep 函数将依次返回返回的值。所以当你打电话的时候,如果它是有效的,你会得到true,如果它不是,你会得到false。

        2
  •  0
  •   shamittomar    15 年前

    你可以这样做:

    function validateRep($rep)
    {
        switch ($rep)
        {
            case 'All':
            case 'Ian':
            case 'Mike':
            case 'Stan':
            case 'Gena':
                $return($rep);
                break;
        }
        return null;
    }
    

    在主代码文件中:

    $rep = validateRep($rep);
    
    推荐文章