Catching PHP Fatal Errors on email autoresponders?

We currently have a software installed on our server which is a PHP HelpDesk system.
It is also responsible for email support email responders and auto-creation of tickets. That is, when customers send emails to support@whatever.com, it creates the HelpDesk ticket and also sends back to the customer telling them that the ticket is created together with the details.

The problem is, there are instances where the autoresponder fails and throws the below error intermittently and randomly:

PHP Fatal error: Allowed memory size of 536870912 bytes exhausted
(tried to allocate 83 bytes) in
/home/whatever/public_html/support/__swift/library/class.SWIFT_Base.php
on line 16

We checked the file and we are confident there is nothing wrong with the code (it works much more often than it fails).

So we are wondering if there is a way to catch the PHP errors encountered by this file (or any PHP file) and run an error script routine.
We plan to create a back-up email saying something like: “Sorry, something went wrong while processing your request. Please try again.”

Any ideas? Thanks in advance!

In my experience, memory exhaust only happens when you have an infinite loop when you are trying to extract data from the database and have no error handling. Normally happens when you have a while loop with no if statement. What happens is when a user/client looks for a certain ID number/record. If the ID/record exists, the exhaust error will be ignored because it actually exists and therefore it will be thrown inside of the while loop and doing the stuff you want it to do. Now if the ID/record doesn’t exist, there is nothing to tell PHP that the ID doesn’t exist and that PHP should stop looking for it. It’ll keep going through the while loop looking to see if the ID/record exists forever until it causes the exhaust error.

On line 16 or before that, check to see if it’s extracting data using a SELECT query. If so, you’ll have to check to see if there isn’t any num_rows (mysqli_*) or rowCount (PDO) before line 16 and see if there’s a while loop just standing there. You’ll have to wrap that while loop in an if statement using either num_rows (mysqli_*) or rowCount (PDO) to fix the memory exhaust error.

thanks for your response. the current script does not go through a database query loop.
our tech team has already ruled out that it is caused by the code itself - but more likely to just be caused by just some intermittent server instability.

so we are just wondering if there is a way to catch fatal errors (like the one above) for a certain script and call another script (e.g. a “please try again” emailer).

Could you post the code in that file leading up to and after line 16. Perhaps 10 or so lines including like 16 would suffice.

thanks oddz for responding. the strange thing is, there is nothing there (from my point of view at least) from line 1 to 16 that can drain the memory. It is just actually the start of a class declaration.
see below from line 1 to 16 which is actually a Kayako code.
we have already reached out to Kayako, and we have more or less ruled it out as a server issue.

<?php
/**
* ###############################################
*
* SWIFT Framework
* _______________________________________________
*
* @package        SWIFT
* @copyright    Copyright (c) 2001-2014, Kayako
* @license        http://www.kayako.com/license
* @link        http://www.kayako.com
*
* ###############################################
*/

abstract class SWIFT_Base {

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.