dongzhenbi8919 2018-04-30 15:37
浏览 64
已采纳

什么会导致isset()和in_array()错误地评估?

EDIT:

A developer broke routing. It affected my page in a way that caused it to fail on production, which is why it acted differently than other servers using the mailer.

So, we have an array called $mailSettings. This array handles the default values, if new values are not supplied to the mailer. This is set up here:

     $mailSettings = array(
        "TOADDRESS" => $wuMail_Config->DefaultToAddress,
        "TONAME" => $wuMail_Config->DefaultToName,
        "CC" => $wuMail_Config->DefaultCC,
        "BCC" => $wuMail_Config->DefaultBCC,
        "SUBJECT" => $wuMail_Config->DefaultSubject,
        "MESSAGE" => $wuMail_Config->DefaultMessage,
        "HTML" => $wuMail_Config->DefaultHTML,
        "FROMADDRESS" => $wuMail_Config->DefaultFromAddress,
        "FROMNAME" => $wuMail_Config->DefaultFromName,
        "REPLYADDRESS" => $wuMail_Config->DefaultReplyAddress,
        "REPLYNAME" => $wuMail_Config->DefaultReplyName
    );

Now, in the next part - we analyze the submitted data... In this specific case, we are only submitted a few custom fields and relying on defaults for the rest.

        $mailData = array(
            "TOADDRESS" => $email,
            "TONAME" => $username,
            "SUBJECT" => "Welcome to SiteName! Please verify your email address.",
            "MESSAGE" => $message,
            "HTML" => $message
        );

So, what we do is this: We take $mailData, and we convert each key name to the uppercase version. So if they submit "toAddReSs", it will be evaluated as "TOADDRESS",in all uppercase lettering - to match the $mailSettings array. We do that here...

foreach($mailData as $submittedOption => $submittedValue){
        $submittedOptionUPPER = strtoupper($submittedOption);
        $submittedOpts[] = $submittedOptionUPPER;
        if(in_array($submittedOptionUPPER, $mailSettings)){
            $mailSettings[$submittedOptionUPPER] = $data[$submittedOption]; 
        } else {
            echo "Mail Configuration Error"; 
        }
    }

I have also tried if(isset($mailSettings[$submittedOptionUPPER]){ and that does not work either. When I print_r $mailData & $mailSettings, both are exactly the same as above (with default values being correctly pulled from our config file).

The issue is that I can do print_r for both arrays RIGHT ABOVE the eval (isset or in_array) and they appear fine... but on this server (and none of our other servers), the code goes into echo "Mail Configuration Error." I cannot (for the life of me), figure this out... and so it's StackOverflow time.

Is there any reason isset AND in_array would both fail on something (on only one specific server)?

Other facts to know...

  1. $mailSettings is inside a function wuMail($mailData){ }
  2. $mailData gets passed to wuMail() from any page that includes the wuMail file (for configuration/function)
  3. On other servers, wuMail($mailData) results in no errors. On this server, it thinks that the values do not exist in array (aka it thinks the developer tried to use wuMail() incorrectly by passing an incorrect key) ~ We do this for safeguarding against developer errors so that we can easily find pages incorrectly using wuMail if that's the case...well, we thought that was the case - and found that nope, the function is evaluating incorrectly.

Any ideas?

  • 写回答

1条回答 默认 最新

  • duanpaxin3531 2018-04-30 15:49
    关注

    You need to use array_key_exists($submittedOptionUPPER, $mailSettings) or isset($mailSettings[$submittedOptionUPPER]) to check the key, however I think what you are doing boils down to this:

    // covert keys to uppercase
    $mailData = array_change_key_case($mailData, CASE_UPPER);
    
    // check if there are keys in $mailData that aren't in $mailSettings
    if(array_key_diff($mailData, $mailSettings) {
        echo "Mail Configuration Error"; // do something else?
    }
    // merge $mailData into $mailSettings
    $mailSettings = array_merge($mailSettings, $mailData);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器