0

There is an error on server side code ( PHP ), I know the file but it's extremely hard to debug as there are no any error messages. All what I see is response header returns: Connection:close. No logs or anything

Is there any tool that I could use to detect or debug this on server side? To see where error happened or script interrupted?

Bojan Savic
  • 337
  • 1
  • 3
  • 12

2 Answers2

0

You can manually put in some echo statements before key areas where you think the script may be failing. Try putting error_reporting(E_ALL); at the top of your scripts also.

half-fast
  • 302
  • 1
  • 7
0

I have the code below in an include file (slightly modified this one to remove custom code). You can use it to log error messages to a database. It uses a class to handle DB interaction, but you can change that to your own implementation. Or log it to a file.

It also outputs information to the browser, but you can discard this aswell in a production environment.

    <?php
    set_error_handler("ErrorHandler"); 
    register_shutdown_function('ShutdownHandler');

    function ShutdownHandler() {
                    $error = error_get_last();
                    if($error !== NULL){
                            switch ($error['type']) {
                                case E_WARNING:exit;
                            }
                            $context = get_defined_vars();
                            ErrorHandler($error['type'],'(SHUTDOWN) ' . $error['message'], $error['file'],$error['line'],$context);
                    }
            }

    function ErrorHandler($error_level,$error_message, $error_file,$error_line,$error_context) 
    {
        $msg = "";
        $type = FriendlyErrorType($error_level);

        switch ($error_level) {
            case E_USER_ERROR:
                    $msg .= "<div style='border:1px solid #F00;padding:10px;margin:10px;'>";
                    $msg .= "<h1>Error</h1>";   
                    $msg .= "<p><b>[".$error_level."]</b>: ". $error_message . "</p>";
                    $msg .= "<p><b>File</b>: ". $error_file . "<br/>";
                    $msg .= "<b>Line</b>: ". $error_line . "</p></div>";

            case E_USER_WARNING:
                    $msg .= "<p>Warning -> <b>[".$error_level."]</b>".$error_file." on line ". $error_line .": ". $error_message . "</p>";
                    break;

            case E_USER_NOTICE:
                    $msg .= "<p>Notice -> <b>[".$error_level."]</b>".$error_file." on line ". $error_line .": ". $error_message . "</p>";
                    break;

            default:
                    $msg .= "<p>".$type." -> <b>[".$error_level."]</b>".$error_file." on line ". $error_line .": ". $error_message . "</p>";
                    break;
        }

        $context = print_r($error_context, true);
        $sVars = print_r($_SERVER, true);
        $pVars = print_r($_POST, true);
        $qVars = print_r($_GET, true);

        //add to database
        //custom class to add info to a database. You need to alter this to your own INSERT method.
        //ct2database::query("INSERT INTO ct2_errors (`DateCreated`,`Type`,`Level`,`Message`,`Line`,`File`,`Context`,`ServerVars`,`PostVars`,`QueryVars`) VALUES ('$1','$2','$3','$4','$5','$6','$7','$8','$9','$10')", MysqlDate(), $type, $error_level,$error_message,$error_line,$error_file,$context, $sVars, $pVars, $qVars);

        if ($error_level == E_USER_ERROR)
            exit;
    }

    function FriendlyErrorType($type)
    {
            switch($type)
            {
                    case E_ERROR: // 1 //
                            return 'E_ERROR';
                    case E_WARNING: // 2 //
                            return 'E_WARNING';
                    case E_PARSE: // 4 //
                            return 'E_PARSE';
                    case E_NOTICE: // 8 //
                            return 'E_NOTICE';
                    case E_CORE_ERROR: // 16 //
                            return 'E_CORE_ERROR';
                    case E_CORE_WARNING: // 32 //
                            return 'E_CORE_WARNING';
                    case E_CORE_ERROR: // 64 //
                            return 'E_COMPILE_ERROR';
                    case E_CORE_WARNING: // 128 //
                            return 'E_COMPILE_WARNING';
                    case E_USER_ERROR: // 256 //
                            return 'E_USER_ERROR';
                    case E_USER_WARNING: // 512 //
                            return 'E_USER_WARNING';
                    case E_USER_NOTICE: // 1024 //
                            return 'E_USER_NOTICE';
                    case E_STRICT: // 2048 //
                            return 'E_STRICT';
                    case E_RECOVERABLE_ERROR: // 4096 //
                            return 'E_RECOVERABLE_ERROR';
                    case E_DEPRECATED: // 8192 //
                            return 'E_DEPRECATED';
                    case E_USER_DEPRECATED: // 16384 //
                            return 'E_USER_DEPRECATED';
            }
            return "";
    } 
    ?>
Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72