3

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.

ficuscr
  • 6,975
  • 2
  • 32
  • 52
voskys
  • 121
  • 1
  • 9
  • 2
    Whether you can set this stuff in .htaccess first of all depends on how you are using PHP with your web server, whether as an Apache module (only then it will work), or in some other way ... – CBroe May 11 '18 at 14:46
  • 1
    "Why is not working in .htacess?" - `.htaccess` has 2 `c`'s. (At first I thought that was just a typo, but all 5 instances are spelt the same - wrong - way?!) – MrWhite May 11 '18 at 15:29
  • @MrWhite Fixed it. I thought something looked strange before but couldn't put finger on it Too consistently wrong – ficuscr May 11 '18 at 16:08
  • @ficuscr, (not native spelling hahah) :-D – voskys May 11 '18 at 16:52
  • @ficuscr Curious, I thought you were the OP when you said you'd "fixed it"!? If the OP had spelt it wrong (not the first time) then that could well have been the cause of this problem. (?) – MrWhite May 16 '18 at 22:13

2 Answers2

3

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.

ficuscr
  • 6,975
  • 2
  • 32
  • 52
  • Thanks, it answered another implicit question that I have. But, why the solution in the link didn't work for me? Any idea? – voskys May 11 '18 at 16:54
  • 1
    `AllowOverride`? Not sure. For one I that only works with mod_php, not *CGI. Not sure on your setup. Anyway, just not a good approach. You should entirely avoid use of .htaccess! Too much overhead, and there are alternatives. (Google a [blog](https://www.eschrade.com/page/why-you-should-not-use-htaccess-allowoverride-all-in-production/) post on the topic) – ficuscr May 11 '18 at 16:59
1
  1. Why is not working in .htaccess?

Whether you can set PHP flags in .htaccess is dependent on how PHP is installed on your server. PHP would need to be installed as an Apache module.

However, since PHP 5.3, per-directory PHP config files are supported in the form of .user.ini files (note the dot prefix). These take the same form as php.ini files, but work on a per-directory basis in userland code.

For example:

; All errors and warnings only
error_reporting=E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED
  1. Which are the priority of this level error instructions? (php.ini vs htaccess vs code) I guess that order?

Yes, that order (.htaccess or .user.ini). Except that some settings can only be set higher up the chain, eg. in php.ini.

MrWhite
  • 43,179
  • 8
  • 60
  • 84