简体   繁体   中英

What would cause isset() and in_array() to both incorrectly evaluate?

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM