Solved

PHP Script Dying

Posted on 2011-03-10
14
527 Views
Last Modified: 2012-05-11
HI all, I am experiencing some very strange behavior in PHP and could use some help. We are running PHP5.3 as a module for Apache22 .

I have a script that creates a PDF report which takes a while to load. On smaller reports (limited records) the PDF script completes as designed with no errors. However, with larger ones, it just dies with no error or log.

I have set at the top of the script:
ini_set("memory_limit", "500M");      
ini_set("max_input_time", "7200");      
ini_set("max_execution_time", "7200");      

Running subsequent ini_get verifies that the above settings have been set by the api call and we are not running php in safe mode.

For debugging purposes, my php.ini is set to:
error_reporting = E_ALL
log_errors = On

The script dies with a blank white page and no log output is created. Is there anything else I can do to try and determine why the script is dying?
0
Comment
Question by:skymander
  • 7
  • 3
  • 3
  • +1
14 Comments
 
LVL 9

Expert Comment

by:Vampireofdarkness
ID: 35099192
Do you have a log file specified (error_log=?)? If not, set

display_errors=On

in your php.ini. Also,

error_reporting  =  E_ALL | E_STRICT

is more detailed than just E_ALL.

You could always try an echo '1' / echo '2' / echo '3' at steps throughout the PDF generation to see at which point it fails, you may need to flush() output though: http://php.net/manual/en/function.flush.php
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35099287
Have you looked in the system error log? Sometimes when PHP dies the message winds up only in the log and not on the screen no matter what you enable.
0
 
LVL 1

Author Comment

by:skymander
ID: 35099345
I tried error_reporting  =  E_ALL | E_STRICT to no avail. I have also printed each loop, it gets (randomly) to about the 1000th iteration then just stops printing anything.

And yes, my log file is set to a custom log but it also works to have it use the system log which directs it to the apache error log.

I have checked the system logs /var/log/messages, the apache error log, and the apache access log and have not found anything.
0
 
LVL 9

Expert Comment

by:Vampireofdarkness
ID: 35099376
Are you able to see what kind of memory usage httpd gets during execution? (Task Manager) Can you provide an example of code? Does each iteration involve one or more SQL query / are they cached / what is the data size returned? Are you clearing variables each iteration to free memory?
0
 
LVL 1

Author Comment

by:skymander
ID: 35099619
I will try and track the memory usage of httpd. Yes the code performs queries and calculations on each iteration including multiple SQL queries. It works when building a report with 1000 records at a time, but if I run all 3200 it fails. Variables are for the most part cleared on each iteration. I can continue go try and reduce memory usage, but the frustrating thing is there is no logs anywhere which makes all optimizations pretty much a total shot in the dark.
0
 
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 35105234
Hello skymander,

try using max_execution_time = 0;

Also it seems that you might need to track your code somehow..

Also confirm your php setting for error using "phpinfo()"

Hope this will help.

Thanks
0
 
LVL 1

Author Comment

by:skymander
ID: 35106051
Amar,

Yes I have tried ini_set("max_execution_time", 0) and still the script times out.

What do you mean about tracking my code?

phpinfo shows that
display_errors = on
the error log is set
and my logging levels are the same as above
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 35107076
Hello skymander,

What do you mean by script time out?

are you getting any message about timeout like "Max execution time exceeded"?

Regarding tracking you can fire emails from your code starting from top of your page to bottom. Put it at main points where you think problem might be.

Execute script and see how many of them are you getting and from which email it stopped.

than you can review that code.

also you can paste your code here. So someone here can help you regarding the matter.

Thanks.

0
 
LVL 1

Author Comment

by:skymander
ID: 35109827
Amar,

Thanks for the reply. As stated in my original post, the timeout is a blank white page with no output. And nothing is put in the error log (either system or custom).

Because this script works great with a smaller set of data, I am not suspecting so much a bad piece of code, but some sort of resource problem like memory or a timeout. But I have tried to set values for both of those manually (see original post) so I'm either missing something, or something is not taking. The timeout is after about 2 minutes.

I have traced the loops via a print-flush statement and it will get through roughly 1000 of 3200 entries (varies each time), then just stop printing.

Sky
0
 
LVL 9

Expert Comment

by:Vampireofdarkness
ID: 35110403
As before: What size output are you getting on each iteration? Are you properly clearing variables each iteration? Can you provide a code example?
0
 
LVL 1

Author Comment

by:skymander
ID: 35110713
Top reveals that httpd consumes 215MB ram at peak and 17% processor which both seem pretty reasonable given the processing.

Here is my code, but as I said before, it works with smaller data-sets. The problem here is that PHP is dying and I don't have any way of determining why.

require_once("global_includes.php");

ini_set("memory_limit", "800M");      
ini_set("max_input_time", "7200");      
ini_set("max_execution_time", "7200");      

// $dbh and $USER are set in global include
$USERNAME = $USER->name;

$pdf =& new Cezpdf();
$pdf->selectFont('../lib/pdf/fonts/Helvetica.afm');
$pdf->ezSetMargins(0,0,0,0);

// Set paper coordinates
$com_cols = 3;
$x_pos = array(24,224,422);
$y_interval = 72;
$original_y = 735;
$curCol = 0;

// Pull list of customers
$olist = new CCustomerList($dbh, "customer", $USER);
$olist->processFormConditions($_POST); // Used to filter fields like date_entered>'1/1/2009'
$olist->getObjects();
$num = $olist->getNumObjects();
for ($i = 0; $i < $num; $i++)
{
      $line_buf = "";
      $obj = $olist->getObject($i); // object data is not loaded until this function is called. The list just holds an array of ids
      $name = $obj->getValue("salutation");
      if (!$name)
            $name = CustGetName($dbh, $obj->id);
      
      $street = $obj->getValue('street');
      $street2 = $obj->getValue('street2');
      $city = $obj->getValue('city');
      $state = $obj->getValue('state');
      $zip = $obj->getValue('zip');
      
      if (!$street || !$zip)
      {
            $street = $obj->getValue('business_street');
            $street2 = $obj->getValue('business_street2');
            $city = $obj->getValue('business_city');
            $state = $obj->getValue('business_state');
            $zip = $obj->getValue('business_zip');
      }
      
      if ($curY < 36)
      {
            $pdf->ezNewPage();
            $curY = $original_y;
            $curCol = 0;
      }
      
      $pdf->addText($x_pos[$curCol], $curY, 10, $name);
      $pdf->addText($x_pos[$curCol], $curY - 12, 10, $street);
      if ($street2)
      {
            $pdf->addText($x_pos[$curCol], $curY - 24, 10, $street2);
            $pdf->addText($x_pos[$curCol], $curY - 36, 10, "$city, $state $zip");
      }
      else
            $pdf->addText($x_pos[$curCol], $curY - 24, 10, "$city, $state $zip");
            
      if ($curCol >= $num_cols)
      {
            $curY -= $y_interval;
            $curCol = 0;
      }
      else
            $curCol++;

      $olist->unsetObject($i); // cleans up all data for this selected object
}

// Print results to browser
$pdf->ezStream();
0
 
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 35121264
Hello skymander,

one more thing you can try is following.

Instead of creating pdf, create a HTML table and see if that table displays correctly. with your data of generating PDF report.

if something is wrong you might be able to get hint about it in that page.

Thanks.
0
 
LVL 1

Accepted Solution

by:
skymander earned 0 total points
ID: 35150243
Well I'm not sure what the problem was, but we resolved it by putting the PDF generation in a background process though gearman, storing the results in a temporary file, and then redirecting the end-user once the job completed.

Thank you all for your help.
0
 
LVL 1

Author Closing Comment

by:skymander
ID: 35178798
We were unable to get anything to log the script dying - still a mystery. Instead we created a work-around using background processes.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
spacing 5 29
Wordpress update causing pages to crash 1 19
Codiing Non-Existent Links 4 24
Wordpress Body Class 5 11
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now