www.webdeveloper.com
Results 1 to 4 of 4

Thread: how do I display a meaningful error message in PHP in case of out-of-memory?

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Posts
    2

    how do I display a meaningful error message in PHP in case of out-of-memory?

    My company is running an internal MediaWiki site on LAMP, which is actively used and has a number of custom modifications. Some users write really big DPL queries, and some of those queries actually exceed the configured memory limit per request.

    The problem is that when PHP runs out of memory, it simply returns an HTTP error 500 and a blank page. I am trying to install a custom error handler which would return something useful to the user, e.g. "your query is too big and exceeded the alotted memory limit". I looked up the docs and found how it (supposedly) can be done; the problem is that the solution simply doesn't work. here is what i am trying to do:

    PHP Code:
    register_shutdown_function ('shutdown_handler');

    function 
    shutdown_handler () {
        
    $err error_get_last();
        if (
    $err && $err['type'] && ($err['type'] > 0) && ($err['type'] < 10)) {
        
    error_reporting(-1);
        
    ini_set('display_errors'1);
        
    ini_set('display_startup_errors'1);
        
    $message sprintf ("*** Error %s: %s\n"$err['type'], $err['message']);
        
    error_log ($message);

        
    header('HTTP/1.1 200'true); 
        
        print (
    $message);

        
    error_log ("Buffer contents:\n\n" ob_get_contents());

        
    flush ();
        
    ob_end_flush();
        exit ();
        }

    Basically a lot of debug code in there. I trap the error, and i log all the pertinent into via the "error_log". I can set the response headers and HTTP code, and the browser receives it (in this case, I was setting it to 200 just because I felt like it). But no matter what I do, I cannot get any output to display to the user in their browser!

    That "print" statement should work. The output string is in the buffer, I can see it in the buffer dump, and I am flushing the buffer -- but the browser simply receives no HTML content, nothing. Blank page.

    Any ideas?

  2. #2
    Join Date
    Jul 2007
    Location
    Wisconsin
    Posts
    468
    i see the flush() and ob_end_flush(), but I don't see where the output buffering is started with ob_start().

  3. #3
    Join Date
    Nov 2012
    Posts
    2
    Hmmm, I thought buffered output was on by default. Anyway, I put in ob_start() right after "if" line, but that made no difference -- I am still seeing no output in the browser. i can set HTTP headers and response code, but not send back HTML.

    Plus, I know buffering was working because ob_get_contents() was returning the buffer contents as set by print. I just can't seem to get the buffer contents actually sent to the browser...

  4. #4
    Join Date
    Nov 2012
    Posts
    14
    PHP sends the http header by itself. Just echo the text you want sent to the user.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles