سؤال

For PHP error handling with the aim of "only admin sees the warnings, errors etc.";

I applied the steps below:

  1. I deleted error_reporting(-1); command from my index.php
  2. I added rows below into my .htaccess which is just under public_html folder
  3. I created error_modes folder in my public_html folder
  4. I created .htaccess file in error_modes folder
  5. I set the permissions of the error_modes folder as 777, writable.
  6. intentionally, I wrote <?php 'should see this error in log file' ?> in my footer.inc.php page. Please note that I didn't wrote ; character at the end.

Despite the intentional php syntax error in my footer.inc.php page, no php_error.log file is created!

and I saw that should see this error in log file string is printed in my footer.inc.php page. So php worked despite a syntax error !?

I also added my whole .htaccess code below. (this is the one that is just under public_html)

fyi: I don't have access to php.ini and I don't have any pre-set .log file. PHP version is 5.4.

Can you please correct me? Thanks. Best Regards.

added commands into public_html > .htaccess for error handling

php_flag  log_errors on
php_flag display_errors off
php_value error_log  /home/my_user_number/public_html/error_modes/php_error.log
php_value error_reporting -1

codes in error_modes > .htaccess

Order allow,deny
Deny from all

whole codes in public_html > .htaccess

RewriteEngine On
RewriteBase /

#always use www - redirect non-www to www permanently
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


# hotlink protection
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mydomain.p.ht [NC]
RewriteRule \.(jpg|jpeg|png|gif|css|js)$ - [NC,F,L]

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# File caching is another famous approach in optimizing website loading time
<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>

# disable directory browsing
Options All -Indexes

# secure htaccess file
<Files .htaccess>
 order allow,deny
 deny from all
</Files>

# secure password file
<Files .mypassword>
 order allow,deny
 deny from all
</Files>

# secure spesific files
<Files secret.php>
 order allow,deny
 deny from all
</Files>

# secure spesific files
<Files secret2.php>
 order allow,deny
 deny from all
</Files>

#SEO friendly linking
RewriteRule ^sitemap.xml$ sitemap.php [L]
RewriteRule ^articles/(.+)/(.+)$ index.php?page=articles&subject=$1&object=$2 [L]
RewriteRule ^articles/(.+)$ index.php?page=articles&subject=$1 [L]
RewriteRule ^labels/(.+)$ index.php?page=labels&subject=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)$ index.php?page=$1 [L]

#error handling
php_flag  log_errors on
php_flag display_errors off
php_value error_log  /home/my_user_number/public_html/error_modes/php_error.log
php_value error_reporting -1
هل كانت مفيدة؟

المحلول

Please try to do the following:

In .htaccess

    # supress php errors
    php_flag display_startup_errors off
    php_flag display_errors off
    php_value docref_root 0
    php_value docref_ext 0


    # enable PHP error logging
    php_flag  log_errors on
    php_value error_log  /correct_path_to_your_website/error_modes/PHP_errors.log


    # general directive for setting php error level
    php_value error_reporting -1

In php file

Instead of intentional mistake in you wrote in your php file you can try doing something like:

    <?

    echo $_SERVER['DOCUMENT_ROOT']; // this will enable you to see 
                                    // the correct path to your website dir 
                                    // which should be written in .htaccess 
                                    // instead of correct_path_to_your_website
                                    // (check it just in case)

    $foo = $bar['nope'];// this should generate notice 

    call_undefined(); // this should generate fatal error


    ?>

Worked good with me)

Hope it'll help.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top