0

我认为这个问题是不言自明的,这里是代码:

HTTP 请求:localhost/execute.php?password=testing&command=create&api=api

这是执行代码的一部分。

try 
{
    $TFA_SAMP->createUser($_GET['email'], $_GET['cellphone'], $_GET['area_code']);
}
catch(Exception $createError)
{
    echo $createError->getMessage();
}

这是类方法:

function createUser($email, $cellphone, $areaCode = 1)
{   
    if(!isset($email))
        throw new BadMethodCallException('(CTFA_SAMP->createUser) email ('. $email .') is missing.');
        
    if(!isset($cellphone))
        throw new BadMethodCallException('(CTFA_SAMP->createUser) cellphone ('. $cellphone .') is missing.');

    $authyLibrary = new Authy_Api($this->API, $this->connectionURL);
    $requestResult = $authyLibrary->registerUser($email, $cellphone, strval($areaCode));
    
    if($requestResult->ok())
    {
        echo $requestResult->id();
    }
    else
    {
        foreach($requestResult->errors() as $field => $message) 
            echo "$field = $message";
    }
}   

PHP 页面打印:

注意:未定义索引:第 46 行 D:\xampp\htdocs\tfasamp\execute.php 中的电子邮件

注意:未定义索引:第 46 行 D:\xampp\htdocs\tfasamp\execute.php 中的手机

注意:未定义索引:第 46 行 D:\xampp\htdocs\tfasamp\execute.php 中的 area_code

(CTFA_SAMP->createUser) 电子邮件 () 丢失。

当我使用异常来显示它们时,如何防止 PHP 给我这些通知?

4

1 回答 1

4
$TFA_SAMP->createUser($_GET['email'], $_GET['cellphone'], $_GET['area_code']);
                      ^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^

在此处访问不存在的变量。没有人关心您稍后检查isset完全不同的变量并抛出异常,问题出在上面一行。你需要在那里修复它。例如:

$args = $_GET + array('email' => null, 'cellphone' => null, 'area_code' => null);
$TFA_SAMP->createUser($args['email'], $args['cellphone'], $args['area_code']);

或者,isset在此处使用语句并为丢失的用户输入引发异常。

基本上,涉及的代码$_GET处理完全不可预测的用户输入。这是您需要检查现有或不存在值的第一道防线。您不能将此作为责任纳入稍后出现的代码中。

于 2013-12-29T19:56:09.870 回答