Search code examples
phparrayssessioncookiesarray-key-exists

array_key_exists throwing error: The second argument should be either an array or an object


I checked all 29 previous posts tagged with array_key_exists and I cannot find a specific question answered that deals with my issue. Our server has recently been updated and we've advanced to PHP 5.2.17 (and yes I know that's still behind, but we're fixing issues as we continue to advance, and 5.3 caused way too many issues for handling at once, let alone 5.4).

Our webpages are throwing an error message tied to array_key_exists:

[ERROR][2][array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object]

else if(array_key_exists("ACCOUNT", $_SESSION) && $_SESSION["ACCOUNT"] == $target){
    // do nothing, we are a-ok
}

In the above code (I think) we're checking to see if a session has already been set and exists for the present account. If so we do nothing. Otherwise we set the session in another else statement after this.

$_SESSION["ACCOUNT"] is being set in a cookie. The value "ACCOUNT" is the subdomain which is also used to identify the account in the database. Here are the lines from the cookie which shows the account is set. The account does exist.

SESSION[ACCOUNTID] = 39
SESSION[ACCOUNT] = demo
SESSION[PAIDACCOUNT] = 0

My question is how should that line of php now be coded so as not to throw that error?

Thanks!


Solution

  • You should use isset instead

    else if(isset($_SESSION["ACCOUNT"]) && $_SESSION["ACCOUNT"] == $target){
        // do nothing, we are a-ok
    }