cancel
Showing results for 
Search instead for 
Did you mean: 

array_key_exists() error in Observer.php on line 166

array_key_exists() error in Observer.php on line 166

Hello i am currently receiving the following error every 0-1 seconds in my var/log/system.log files however i am not aware of any issues currently affecting the site. The error 

 

Error:

ERR (3): Warning: array_key_exists() expects parameter 2 to be array, null given in app/code/core/Mage/Captcha/Model/Observer.php on line 166

1 REPLY 1

Re: array_key_exists() error in Observer.php on line 166

We have this same issue as well, so I decided to look into it.  It appears that the reason that this is happening is because a null value is being passed to the method array_key_exsists.

 

In file: app/code/core/Mage/Captcha/Model/Observer.php

 public function checkUserLoginBackend($observer)
    {
        $formId = 'backend_login';
        $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
        $loginParams = Mage::app()->getRequest()->getPost('login');
        $login = array_key_exists('username', $loginParams) ? $loginParams['username'] : null;
        if ($captchaModel->isRequired($login)) {
            if (!$captchaModel->isCorrect($this->_getCaptchaString(Mage::app()->getRequest(), $formId))) {
                $captchaModel->logAttempt($login);
                Mage::throwException(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
            }
        }
        $captchaModel->logAttempt($login);
        return $this;
    }

You can see on line 166 that it is checking to see if the key exsists.  This Observer method is triggered from:

<admin_user_authenticate_before>
    <observers>
        <captcha>
            <class>captcha/observer</class>
            <method>checkUserLoginBackend</method>
        </captcha>
    </observers>
</admin_user_authenticate_before>

the problem with this, is there is no post request in the observer when its being check, hence, nothing comes back.  This results in the error, because the method is expecting an array and gets nothing.  What i did to fix ours is to override this file.  you can do that by putting a copy of the file here:

 

app/code/local/Mage/Captcha/Model/Observer.php

 

and changing the method to add the error control operator to supress the error.

 

/**
     * Check Captcha On User Login Backend Page
     *
     * @param Varien_Event_Observer $observer
     * @return Mage_Captcha_Model_Observer
     */
    public function checkUserLoginBackend($observer)
    {
        $formId = 'backend_login';
        $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
        $loginParams = Mage::app()->getRequest()->getPost('login');
        $login = @array_key_exists('username', $loginParams) ? $loginParams['username'] : null;
        if ($captchaModel->isRequired($login)) {
            if (!$captchaModel->isCorrect($this->_getCaptchaString(Mage::app()->getRequest(), $formId))) {
                $captchaModel->logAttempt($login);
                Mage::throwException(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
            }
        }
        $captchaModel->logAttempt($login);
        return $this;
    }

Rember it is always bad practice to change core files.  Magento gives you a very nice way of overriding core files by putting them in your local directory.   If you have any issues, or need any help, just let me know.

 

And if someone has a better solution, please let me know Smiley Happy

 

I apologize for the unglyness of the code display. it appears the Magento has a missing CSS class, and their forum wont allow me to style the element myself.