Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 380071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:57:12+00:00 2026-05-12T14:57:12+00:00

I’m trying to read a file when ever an error occurs within my scripts

  • 0

I’m trying to read a file when ever an error occurs within my scripts so I can throw a custom error page.

When using ob_start/set_error_handler I am unable to use file_get_contents or ob_start within the callback to get the contents of my error template.

Does anyone know how I can output my custom template (and using eval) within the callback?


Edit: Some code

error_reporting(E_ALL);
ini_set("display_errors", 1); 

function fatal_error_handler($buffer) {
  if (ereg("(error</b>:)(.+)(<br)", $buffer, $regs) ) {
    $ErrorString = preg_replace("/<.*?>/","",$regs[2]);
    error_log($ErrorString);

    $template = file_get_contents(sprintf('%s/errors/Error.php', TEMPLATES));
    return eval(sprintf('?>%s<?', $template);
    //return "ERROR CAUGHT check log file";
  }
  return $buffer;
}

function handle_error ($errno, $errstr, $errfile, $errline)
{
    error_log("$errstr in $errfile on line $errline");
    if($errno == FATAL || $errno == ERROR){
        ob_end_flush();
        echo "ERROR CAUGHT check log file";
        exit(0);
    }
}

ob_start("fatal_error_handler");
set_error_handler("handle_error");

The above just displays an empty page.

error_reporting(E_ALL);
ini_set("display_errors", 1); 

function fatal_error_handler($buffer) {
  if (ereg("(error</b>:)(.+)(<br)", $buffer, $regs) ) {
    $ErrorString = preg_replace("/<.*?>/","",$regs[2]);
    error_log($ErrorString);

    ob_start();
    include(sprintf('%s/errors/Error.php', TEMPLATES));
    $template = ob_get_contents();
    ob_end_clean();
    return eval(sprintf('?>%s<?', $template));
    //return "ERROR CAUGHT check log file";
  }
  return $buffer;
}

function handle_error ($errno, $errstr, $errfile, $errline)
{
    error_log("$errstr in $errfile on line $errline");
    if($errno == FATAL || $errno == ERROR){
        ob_end_flush();
        echo "ERROR CAUGHT check log file";
        exit(0);
    }
}

ob_start("fatal_error_handler");
set_error_handler("handle_error");

and the above gives the following:

Fatal error: ob_start() [<a href='ref.outcontrol'>ref.outcontrol</a>]: Cannot use output buffering in output buffering display handlers in /var/www/index.php on line 16
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:57:12+00:00Added an answer on May 12, 2026 at 2:57 pm

    You have to :

    • know that file_get_contents returns false when it encounters an error.
      • which means you can test its return value,
      • and if it returned false, you can display your template
    • also be careful : when there’s a problem, it generates an error, and you don’t want that one displayed
      • this is the kind of situation for which you can use the @ operator,
      • to “mask” the error — you test if it returned false, anyway

    It would give that kind of code :

    $result = @file_get_contents('non-existing-file');
    if ($result === false) {
        echo "An error has occured";
    }
    

    Which, in my case, displays :

    An error has occured
    

    Now, up to you to display your error template, depending on the Framework you might be using and all that 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 279k
  • Answers 279k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check out the 3rd Example from the MSDN Sample: http://code.msdn.microsoft.com/vssdkwcftools… May 13, 2026 at 3:21 pm
  • Editorial Team
    Editorial Team added an answer Your first thought is the correct one. Split the 2… May 13, 2026 at 3:21 pm
  • Editorial Team
    Editorial Team added an answer As a modification to Martin B's answer, consider using the… May 13, 2026 at 3:21 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.