简体   繁体   中英

how to enable error log if i don't have access to php.ini

I have to mention that this question have been asked before and someone answered "You can still add configuration settings using a PHP file and not publish that into a public place (folder)." but i'm not quite sure what is that means so I'm asking if i don't have access to php.ini can i put this code in a php file and include it inside other files or there is a proper way for doing this

<?php
ini_set("error_reporting",E_ALL);
ini_set("log_error",1);
ini_set("error_log","error.log");
?>
<?php
error_reporting(E_ALL); //sets logging filter to log all errors
//error_reporting(E_ALL & ~E_NOTICE); //sets logging filter to all except NOTICE, useful when your coding style is rather hacky (like mine)
ini_set('display_errors',1); //displays error output onpage

This should be sufficient.

Be warned that if an error occurs that generates output headers will be sent! You cannot set cookies after this unless you use output buffering to catch possible errors and preventing the sending of headers on unwanted moments.

Edit

If you have or want to prevent problems caused by headers already been sent you can buffer the output and echo the output at the end of the page.

Code with problem:

<?php
    error_reporting(E_ALL); //sets logging filter to log all errors
    ini_set('display_errors',1); //displays error output onpage
    if($a/*is not set*/){ //echoes to page: undefined variable in /path/index.php at line linenumber
        //code...
    }
    setcookie('testcookie','testvalue', time+3600); //error headers have already been sent so this won't work

Code with fix

  <?php
    ob_start();
    error_reporting(E_ALL); //sets logging filter to log all errors
    ini_set('display_errors',1); //displays error output onpage
    if($a/*is not set*/){ //echoes to buffer: undefined variable in /path/index.php at line linenumber
        //code...
    }
    setcookie('testcookie','testvalue', time+3600); //Works!
    //Rest of page
    echo ob_get_clean(); //this is the last line (add <?php if it's not already in a php tag)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM