How would one include a footer on a page if PHP has stopped executing during an error?

Dani AI

Generated

Brief practical guidance that ties the existing replies together and fills gaps left by the thread: ’s shutdown idea is a useful safety net and ’s “put footer before every exit” will work but is tedious and fragile. For user-driven validation errors the clean design is to avoid killing the script mid-template. Validate early, collect error messages, and render the full page (header + body + footer) once — this keeps layout logic in one place and avoids repeating includes around every exit/die.

A minimal pattern that keeps footer logic central (no repeated includes) is to have a single render routine and return from the controller when something fails:

function renderPage($bodyHtml) {
  echo $bodyHtml;
  renderFooter();   // application-defined function or template call
}

if (!$valid) {
  renderPage(renderValidationErrors($errors));
  return;           // return instead of exit()
}

// continue normal processing...
renderPage($mainContent);

Use register_shutdown_function as a fallback for unexpected fatal runtime errors (this is what suggested), but note it’s a safety net, not a substitute for proper flow control. Pair it with output buffering and check error_get_last() inside the shutdown handler; if a fatal error happened, discard partial output and emit a minimal error page (with footer) so the user doesn’t see half-rendered HTML. Finally, be aware that parse-time fatal errors or a process kill may prevent shutdown handlers/destructors from running, and that footer output should be skipped for non-HTML responses (JSON, file downloads, AJAX). In production, keep display_errors off and log errors instead.

Recommended Answers

All 8 Replies

you fix the error..

depending on the kind of error you might try to catch it..

That's not what I mean. I mean general user-influenced errors like not entering valid details. I am using an exit function after displaying each error. Does that mean I have to put in include statement before every exit function?

i don't think im really getting what your trying to do, specifically why your using exit() but to do anything after calling exit() you could register a shutdown function:

function shutdown()
{
include 'footer.php';
}
register_shutdown_function('shutdown');

the "OOP way" would be using your __deconstruct() which will be called after exit().

i don't think im really getting what your trying to do, specifically why your using exit() but to do anything after calling exit() you could register a shutdown function:

function shutdown()
{
include 'footer.php';
}
register_shutdown_function('shutdown');

the "OOP way" would be using your __deconstruct() which will be called after exit().

Thanks that's just what I meant :)
If I register that shutdown function it will include the footer after every exit or die command?

I am using exit() after errors to stop the script from proceeding onto the next bit. Am I doing it wrong?

I never used a shutdown function but as the php manual says: "Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit() is called. " it should work as you want it to.

It is not wrong to call exit after an error but depending on the rest of your code it might not be the best way.

I never used a shutdown function but as the php manual says: "Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit() is called. " it should work as you want it to.

It is not wrong to call exit after an error but depending on the rest of your code it might not be the best way.

Don't want to trouble you any further but what other options are there to handle errors apart form using exit()?

Totally depends on your application but you described it as user-influenced errors so my guess is a if/else statement should be sufficient a alternative would be working with exceptions (again this all depends on the rest of your code).

add the footer line before each exit()
Example:

<html> <head></head> <body> <?php include("header.php"); ?> //header file externally designed
<?php
if(condition){
//some code
include("footer.php"); //if a external file for footer is designed
exit();
}
<?php include("footer.php"); ?> </body> </html>

since these footers comes under if/else statement, no chances of displaying them along with the main footer added befor the body end tag. Hope it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.