Showing posts with label WSOD. Show all posts
Showing posts with label WSOD. Show all posts

2014-08-13

How to fix #PHP #BSOD - Blank Screen of Death / #WSOD - the White Screen of Death (PHP Debugging)

You created a PHP file which was running fine. Then you made some changes to improve it and uploaded it to your server. Suddenly, all you get is a blank screen on your browser. Not even an error message!!

What happened ?!?

You have just witnessed the well known PHP BSOD or WSOD.
This happens when error messages are turned off. This would be the default settings on many production PHP servers. You won't want your customers to see all your error messages appearing on their screens.

How to fix this? Basically, we can
1) turn on the error messages display, or
2) create custom error handlers
 
#1 - Turning on error messages

You can change the settings of the php.ini or .htaccess or include the following
at the top of the php file

<?php
ini_set('error_reporting', E_ALL); // or -1 
ini_set('display_errors', 'On');  //On or Off

On Apache web server, we can use the .htaccess file to configure
PHP error messages

php_value display_errors 1
php_value display_startup_errors 1
php_value error_reporting -1 # E_ALL

Or, if you do have access to the php.ini, you can set the error reporting in the php.ini

display_errors = On
display_startup_errors = On 
error_reporting = E_ALL 
  #2 - Custom error handling function

You can define your own handling function. When an error occurs, this custom
function gets called

<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting
        return;
    }

    switch ($errno) {
    case E_USER_ERROR:
        echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
        echo "  Fatal error on line $errline in file $errfile";
        echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
        echo "Aborting...<br />\n";
        exit(1);
        break;

    case E_USER_WARNING:
        echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
        break;

    case E_USER_NOTICE:
        echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
        break;

    default:
        echo "Unknown error type: [$errno] $errstr<br />\n";
        break;
    }

    /* Don't execute PHP internal error handler */
    return true;
}


// you need to register the custom error handler to use it
// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");


// the #old_error_handler can be used to reset the handler

     

 




Github CoPilot Alternatives (VSCode extensions)

https://www.tabnine.com/blog/github-copilot-alternatives/