Post by moneycarlo » Wed Oct 25, 2017 11:57 am

using 2.3.0.2 and in my error log i'm getting an error:
PHP Warning: Division by zero in /system/storage/modification/catalog/controller/product/category.php on line 380

When I go to line 380 in the above file, I have this:
$data['results'] = sprintf($this->language->get('text_pagination'), ($product_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($product_total - $limit)) ? $product_total : ((($page - 1) * $limit) + $limit), $product_total, ceil($product_total / $limit));

Can someone tell me what it's looking at that's causing the error? so far none of the other division by zero threads appear to be the same from what i've found.
thanks!

Active Member

Posts

Joined
Wed Sep 28, 2011 3:40 am

Post by kestas » Wed Oct 25, 2017 2:18 pm

Hi,

Seems you have installed some module where is this formula. this formula is bad. Because division by zero is not allowed (in red).
$data['results'] = sprintf($this->language->get('text_pagination'), ($product_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($product_total - $limit)) ? $product_total : ((($page - 1) * $limit) + $limit), $product_total, ceil($product_total / $limit));

Custom OpenCart modules and solutions. You can write PM with additional questions... Extensions you can find here


Active Member

Posts

Joined
Tue Oct 12, 2010 2:23 am

Post by paulfeakins » Wed Oct 25, 2017 5:22 pm

kestas wrote:
Wed Oct 25, 2017 2:18 pm
Seems you have installed some module where is this formula. this formula is bad. Because division by zero is not allowed (in red).
$data['results'] = sprintf($this->language->get('text_pagination'), ($product_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($product_total - $limit)) ? $product_total : ((($page - 1) * $limit) + $limit), $product_total, ceil($product_total / $limit));
As above, it's a badly written extension. Disable them one by one until you find which one is the cause and then ask for support from the developer.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by Johnathan » Sat Dec 02, 2017 2:55 am

I fixed this for a client today, and just to note, this code is actually in OpenCart 2.3.0.2, so the code itself is not from a mod. It may be being *triggered* by a mod, but the OpenCart code has the actual syntax error in it.

Also, the syntax error is not the 0 in that line of code, but this part:

$product_total / $limit

It occurs when $limit is 0, which is probably what the mod is triggering, since OpenCart doesn't ever load a page with a $limit of 0 (as far as I know). It could also be a bot that is scraping the site and getting the $limit wrong, or leaving it blank.

To fix it, you can make this edit:

Code: Select all

IN:
/catalog/controller/product/category.php

REPLACE ALL INSTANCES OF:
if (isset($this->request->get['limit'])) {

WITH:
if (!empty($this->request->get['limit'])) {

The same issue will occur for any other paginated page with a $limit of 0, so you can make the same edit in the other files in /catalog/controller/product/ if you see the error come from any of those files.

Image Image Image Image Image


User avatar
Administrator

Posts

Joined
Fri Dec 18, 2009 3:08 am


Post by jtos » Tue Mar 23, 2021 1:30 am

Thank you for this fix Johnathan, I was getting this error from a bot scraping the site. Your code has fixed this for when as you say when the bot has the value set to 0 or blank. However if the value is something like "0:0" or "a0", the error persists, is there a better solution that can deal with any value?

Newbie

Posts

Joined
Thu Jun 18, 2020 9:28 pm

Post by Johnathan » Tue Mar 23, 2021 11:13 pm

Try changing the edit to this:

Code: Select all

IN:
/catalog/controller/product/category.php

REPLACE ALL INSTANCES OF:
if (isset($this->request->get['limit'])) {

WITH:
if (!empty((int)$this->request->get['limit'])) {

Image Image Image Image Image


User avatar
Administrator

Posts

Joined
Fri Dec 18, 2009 3:08 am


Post by jtos » Wed Mar 24, 2021 5:07 am

Thanks for this suggestion Johnathan. The results are that it does stop any errors when the limit is set to any text value, however it will generate an error every time you view a category without any limit specified in the URL, such as the default view, and it does this for the first 5 instances of the replaced code. Meaning just viewing a category will generate 5 error logs, each with the error message "PHP Notice: Undefined index: limit in ...catalog/controller/product/category.php on line...".

As it only generated it for the first 5 occurrences and not the 6th/final occurrence, I replaced only the 6th/final instance with the '(int)' version, and the others as the previous '!empty' non-int version, but this performed as the original solution with the division by zero error still there for text values.

Newbie

Posts

Joined
Thu Jun 18, 2020 9:28 pm

Post by Johnathan » Wed Mar 24, 2021 5:43 am

Ah, it must not like being cast to an int before it's checked whether the parameter is set. Try this edit instead:

Code: Select all

IN:
/catalog/controller/product/category.php

REPLACE ALL INSTANCES OF:
if (isset($this->request->get['limit'])) {

WITH:
if (!empty($this->request->get['limit']) && (int)$this->request->get['limit'] > 0) {
    $this->request->get['limit'] = (int)$this->request->get['limit'];

Image Image Image Image Image


User avatar
Administrator

Posts

Joined
Fri Dec 18, 2009 3:08 am

Who is online

Users browsing this forum: No registered users and 65 guests