Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: I need help preventing PHP errors from displaying in the browser

  1. #1
    huluganfantasy is offline Junior Member
    Join Date
    Aug 2016
    Posts
    13

    Default I need help preventing PHP errors from displaying in the browser

    How do I stop PHP errors from displaying to users?

    I've tried this code at the top of my PHP script:

    PHP Code:
    ini_set ('error_reporting', E_ALL ^ E_DEPRECATED);
    ini_set ('display_errors', 'off');
    ini_set ('log_errors', 'On');
    ini_set ('error_log', 'path to my log');

    ...and this code in "Edit .htaccess source:
    Code:
        php_flag display_startup_errors off
        php_flag display_errors off
        php_flag html_errors off
        php_flag log_errors on
        php_flag ignore_repeated_errors off
        php_flag ignore_repeated_source off
        php_flag report_memleaks on
        php_flag track_errors on
        php_value docref_root 0
        php_value docref_ext 0
        php_value error_log /path to my log
        # [see footnote 3] # php_value error_reporting 999999999
        php_value error_reporting -1
        php_value log_errors_max_len 0
    
        <Files errors.log>
         Order allow,deny
         Deny from all
         Satisfy All
        </Files>

    However neither seems to be working as errors still show up on screen. Also, how should I describe the "path to my log" in each case? Right now the path in the PHP file is described relative to the public page that causes the error, and the path in the .htaccess file is described relative to the location of the .htaccess file . Thanks.

  2. #2
    karl94 is offline AlterVista Staff
    Join Date
    Jan 2015
    Posts
    573

    Default

    If your script generates error messages you should fix it, instead of hiding the problem. However, on AlterVista you can't use the ini_set function or php_flag or php_value directives.
    You can use error_reporting, but that hides the problem, you'll still need to fix the errors in your script.
    If you don't know how to fix them, post here the error messages.

  3. #3
    huluganfantasy is offline Junior Member
    Join Date
    Aug 2016
    Posts
    13

    Default

    Quote Originally Posted by karl94 View Post
    If your script generates error messages you should fix it, instead of hiding the problem. However, on AlterVista you can't use the ini_set function or php_flag or php_value directives.
    You can use error_reporting, but that hides the problem, you'll still need to fix the errors in your script.
    If you don't know how to fix them, post here the error messages.
    Please read my post again Karl. I am not "hiding" any problems. I want errors recorded in a log. I can easily check the log and fix any errors. I do not want errors displayed because it puts a map of my site folders, folder names and server-side code in every viewer's browser. That is a security risk, AND very unprofessional. Thanks for your help.

    EDIT It's also not efficient, because I won't see errors other site visitors encounter, unless those errors are recorded in a log. I can't see their browser
    Last edited by huluganfantasy; 09-02-2016 at 08:19 PM.

  4. #4
    huluganfantasy is offline Junior Member
    Join Date
    Aug 2016
    Posts
    13

    Default

    In case I wasn't clear Karl. I want to see errors. Can you please tell me how I can do that WITHOUT also showing sensitive information to the general public.

    EDIT
    Quote Originally Posted by karl94
    You can use error_reporting...
    I just tested:
    PHP Code:
    error_reporting(0);
    ...and unfortunately that does not work either. This is very frustrating.
    Last edited by huluganfantasy; 09-02-2016 at 08:51 PM.

  5. #5
    karl94 is offline AlterVista Staff
    Join Date
    Jan 2015
    Posts
    573

    Default

    Could you please post here the error message you got displayed while the error_reporting call was in place? Where did you place the error_reporting call? You need to have it before any error is triggered, and obviously it won't be effective in the case there is a syntax error in the same file (since the parsing of the script is done before its execution).
    To catch error messages and log them you can use the set_error_handler function, it won't catch fatal errors but you can do that with register_shutdown_function and error_get_last (read http://stackoverflow.com/questions/2...hp-fatal-error).

  6. #6
    huluganfantasy is offline Junior Member
    Join Date
    Aug 2016
    Posts
    13

    Default

    Quote Originally Posted by karl94 View Post
    Could you please post here the error message you got displayed while the error_reporting call was in place? Where did you place the error_reporting call? You need to have it before any error is triggered, and obviously it won't be effective in the case there is a syntax error in the same file (since the parsing of the script is done before its execution).
    To catch error messages and log them you can use the set_error_handler function, it won't catch fatal errors but you can do that with register_shutdown_function and error_get_last (read http://stackoverflow.com/questions/2...hp-fatal-error).
    I don't know if you changed anything, but "error_reporting(0);" is working correctly for me now. I have written an error handler function as well as a register_shut_down function to catch fatal errors, however there is a problem. In order to log errors with a register_shut_down_function, I have to use an absolute path to the log file, because when the script processing is complete the current working directory has changed to the server root. In my developement environment I set a log variable for the function like this:
    PHP Code:
    $errorlog = $_SERVER['DOCUMENT_ROOT'] . "/path/error_file.log";
    How can I do that here in Altervista? The main folder for my site here is just called "/" by your system. there is no name.

  7. #7
    huluganfantasy is offline Junior Member
    Join Date
    Aug 2016
    Posts
    13

    Default

    OK, I got an absolute path using "echo __DIR__" And the shutdown_function logs the errors correctly now, but there is another problem :
    The user will see a blank page. I would like to redirect them to a custom error page. In my development environment I can use:
    PHP Code:
    header('Location: my_custom_page.php');
    header('Status: 500 Internal Server Error');
    It works in my development site, but it's not working on Altervista. Here it just stays on the blank page where the error was, and doesn't redirect them. What should I use here?

  8. #8
    karl94 is offline AlterVista Staff
    Join Date
    Jan 2015
    Posts
    573

    Default

    Could you please provide the complete code that's working on your development environment?

  9. #9
    huluganfantasy is offline Junior Member
    Join Date
    Aug 2016
    Posts
    13

    Default

    This is the code that works in the development environment:

    PHP Code:
    <?php
    // Error handling
    function errorHandler($num, $str, $file, $line) {

    $text = "error {$num} in {$file}, line {$line}: {$str}\n";
    $errorlog = "absolute path to my_log";

    $new = file_exists($errorlog) ? false : true;
    if(
    $handle = fopen($errorlog, 'a')) { //append
    fwrite($handle, $text);
    fclose($handle);
    if(
    $new) { chmod($errorlog, 0755); }
    } else {
    //stuff noting that nothing was logged
    }

    }

    function
    fatalHandler() {
    $error = error_get_last();
    if(
    $error !== NULL && $error['type'] == E_ERROR) {
    errorHandler($error["type"], $error["message"], $error["file"], $error["line"]);
    header('Location: [url]http://huluganfantasy.altervista.org/public/error.php');
    header('Status: 500 Internal Server Error');
    }
    }

    set_error_handler("errorHandler");
    register_shutdown_function("fatalHandler");
    ?>
    Karl, it would save everyone a lot of trouble, if Altervista provided us a direct option to create a log for recording, and to turn of display errors. Maybe you could allow limited use of the ini_set function for only that purpose, or some other alternative? That way even parse errors could be handled.
    Last edited by huluganfantasy; 09-12-2016 at 10:52 PM.

  10. #10
    karl94 is offline AlterVista Staff
    Join Date
    Jan 2015
    Posts
    573

    Default

    Quote Originally Posted by huluganfantasy View Post
    This is the code that works in the development environment:
    Could you please provide the complete code needed to reproduce the issue? Which error are you trying to catch exactly? You should include that in the code, otherwise is difficult to help you.
    Why did you include that if inside fatalHandler? That way you can't catch parse errors, for instance.
    Quote Originally Posted by huluganfantasy View Post
    Karl, it would save everyone a lot of trouble, if Altervista provided us a direct option to create a log for recording, and to turn of display errors. Maybe you could allow limited use of the ini_set function for only that purpose, or some other alternative? That way even parse errors could be handled.
    Unfortunately that is currently not possible

Page 1 of 2 12 LastLast

Posting Permissions

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

SEO by vBSEO