Jump to content

Trying to access array offset on value of type bool in guest.php Prestashop 8.1.5


RicRey

Recommended Posts

Hello everyone, I present this error_log, I know it is just news, but it inflates the error_log file, I would like to know why this happens and solve it...

I tell you that this error started appearing after updating from Prestashop 1.7.8.7 to Prestashop 8.1.5.

I have updated all modules to their versions compatible with PrestaShop 8.1.5

This is the error:

[10-Apr-2024 07:51:22 America/Guayaquil] PHP Notice:  Trying to access array offset on value of type bool in /home/sistegra/public_html/classes/Guest.php on line 145

[10-Apr-2024 07:51:32 America/Guayaquil] PHP Notice:  Trying to access array offset on value of type bool in /home/sistegra/public_html/classes/Guest.php on line 178

And the part you mention of the class guest.php file is the identification of the type of operating system and browser...

Here I put that piece of the function code:

Line 145: return $result['id_web_browser'];

Line 178: return $result['id_operating_system'];

    /**
     * Get browser.
     *
     * @param string $userAgent
     */
    protected function getBrowser($userAgent)
    {
        $browserArray = [
            'Chrome' => 'Chrome/',
            'Safari' => 'Safari',
            'Safari iPad' => 'iPad',
            'Firefox' => 'Firefox/',
            'Opera' => 'Opera',
            'IE 11' => 'Trident',
            'IE 10' => 'MSIE 10',
            'IE 9' => 'MSIE 9',
            'IE 8' => 'MSIE 8',
            'IE 7' => 'MSIE 7',
            'IE 6' => 'MSIE 6',
        ];
        foreach ($browserArray as $k => $value) {
            if (strstr($userAgent, $value)) {
                $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
                SELECT `id_web_browser`
                FROM `' . _DB_PREFIX_ . 'web_browser` wb
                WHERE wb.`name` = \'' . pSQL($k) . '\'');

                return $result['id_web_browser'];
            }
        }

        return null;
    }

    /**
     * Get OS.
     *
     * @param string $userAgent
     */
    protected function getOs($userAgent)
    {
        $osArray = [
            'Windows 10' => 'Windows NT 10',
            'Windows 8.1' => 'Windows NT 6.3',
            'Windows 8' => 'Windows NT 6.2',
            'Windows 7' => 'Windows NT 6.1',
            'Windows Vista' => 'Windows NT 6.0',
            'Windows XP' => 'Windows NT 5',
            'MacOsX' => 'Mac OS X',
            'Android' => 'Android',
            'Linux' => 'X11',
        ];

        foreach ($osArray as $k => $value) {
            if (strstr($userAgent, $value)) {
                $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
                SELECT `id_operating_system`
                FROM `' . _DB_PREFIX_ . 'operating_system` os
                WHERE os.`name` = \'' . pSQL($k) . '\'');

                return $result['id_operating_system'];
            }
        }

        return null;
    }

 

Link to comment
Share on other sites

Hi,
These are not an error but notices that could be removed by disabling debug mode.
The above error occurs only if the query fails or returns no results. We need to ensure that $result contains a valid result before trying to access its elements.

So we could update the return statement as
return $result['id_operating_system'] ?? null;
return $result['id_web_browser']??null;

so if the $result is false or null, it will return null instead of trying to access an array offset on a boolean value, thus avoiding the notice

Link to comment
Share on other sites

  • 4 months later...
On 4/16/2024 at 2:30 PM, Knowband Plugins said:

Hi,
These are not an error but notices that could be removed by disabling debug mode.
The above error occurs only if the query fails or returns no results. We need to ensure that $result contains a valid result before trying to access its elements.

So we could update the return statement as
return $result['id_operating_system'] ?? null;
return $result['id_web_browser']??null;

so if the $result is false or null, it will return null instead of trying to access an array offset on a boolean value, thus avoiding the notice

No, they are notices that fill up the error log on production systems and *aren't* disabled, ever - even when you downgrade to a more sensible reporting level for a production system from the default.
 

E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED


I know it's not a high priority but stuff like this just makes using the software more of a hassle than it really needs to be. It's an example of adding a feature (store statistics) that is intensive to maintain (hard-coded values) and then unsurprisingly not maintaining it....

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...