• Blogs (9)
    • đŸ“± 236 - 992 - 3846

      📧 jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • PHP: 2 tips to avoid logfile warnings

    Blogs20132013-01-29


    1. timezone warning in error log

    When using Apache as webserver, if not setup properly, some apps(like smarty templates) will generate error/warning in error_log. e.g.: when not setup timezone by default, there will be warnings in PHP’s error_log:

    PHP Warning: strftime(): It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date\default_timezone_set() function
_

    This will write into error.log frequently, to make it increasing quickly and badly, which we don’t want and have to avoid it. To prevent from it, it is easy, here are my 2 solutions: (1) in php.ini file, Add timezone ‘America/Vancouver’ to setup default:

    //1. add timezone php config file:
    $ sudo vi /etc/php.ini
    [Date]
    ; Defines the default timezone used by the date functions
    ; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
    date.timezone = 'America/Vancouver';
    
    //2. restart server:
    $ sudo /etc/init.d/httpd restart

    (2) Or, adding the following codes in app config file as a global setting:

    $timezone = "America/Vancouver";
    if(function_exists('date_default_timezone_set'))
      date_default_timezone_set($timezone);

    By the settings, it won’t generate timezone warnings anymore; There will be no waring reports to httpd’s error_log file, so clear up the log file.

    2. redirect script to avoid repeated warnings in error_log:

    PHP scripts could occur this kind of repeated warnings in httpd error_log:

    [ 66.249.74.159] PHP Warning: mysql\fetch_assoc() expects parameter 1 to be resource, boolean given in foo.php on line 246
_

    This kind of scripts are deprecated; however, google search crawler still look up for it, to generate meaning errors repeatedly. To avoid it: just let google crawler to pass it, not execute, to redirect the PHP script:

    // $ vi +246 foo.php:
    public function test($param) {
     //add something like this to return directly instead of executing:
     return array();
     ...
     return mysql_fetch_assoc($res);
    }
    // Or:
    ini_set('date.timezone', $timezone);

    This way will prevent the google’s execute of the function so to avoid the warning output.