Search code examples
php.htaccessinierror-logging

PHP Error log level control: htaccess vs php.ini vs code and virtualhost


I have a virtual host in my WAMP local server, where I set my log file.

I wanted to change my PHP log error level to only warnings and errors.

The best way should be .htaccess, I tried this solution:

How to disable notice and warning in PHP within .htaccess file?

Dind't work (tried others also).

At the end went to php.ini file, however is the less flexible options.

1) Which are the priority of this level error instructions? (php.ini vs htaccess vs code) I guess that order?

2) Why is not working in .htaccess? I just set it on top of .htaccess, and did't work.


Solution

  • My recommendation would be to use an httpd environment variable.

    This can act as a flag to inform your web application about the environment. Say you had a white label product, there it can be useful for example in setting what configuration should be used with a specific vhost.

    <VirtualHost *:80>
      DocumentRoot "/srv/www/foo/public"
      ServerName foo.com
    
      <Directory /srv/www/foo/public>
    
        # production | development | staging
        SetEnv APPLICATION_ENV development
        ...
    

    And then in your PHP code you would access it like:

    <?php
    define('APPLICATION_ENV_LOCAL', 'local');
    define('APPLICATION_ENV_DEVELOPMENT', 'development');
    define('APPLICATION_ENV_STAGING', 'staging');
    define('APPLICATION_ENV_PRODUCTION', 'production');
    
    $app_env = (getenv('APPLICATION_ENV')) ? getenv('APPLICATION_ENV') : false;
    if (empty($app_env) || ! in_array($app_env, array(APPLICATION_ENV_LOCAL, APPLICATION_ENV_DEVELOPMENT, APPLICATION_ENV_STAGING, APPLICATION_ENV_PRODUCTION))) {
        throw new Exception("APPLICATION ENV IS NOT SPECIFIED OR IS INVALID.");
    }
    

    What I'd do is have totally separate config / INI type files that I include based on the environment. Those could determine error reporting behavior, maintain distinct database connections, anything else dependent on the application environment.