繁体   English   中英

使用.htaccess进行PHP错误处理并写入php_error.log文本文件

[英]PHP error handling with .htaccess & writing into php_error.log text file

对于PHP错误处理,其目的是“只有管理员才能看到警告,错误等”。

我应用了以下步骤:

  1. 我删除了error_reporting(-1); 来自我的index.php命令
  2. 我在public_html文件夹下的.htaccess添加了以下行
  3. 我在public_html文件夹中创建了error_modes文件夹
  4. 我在error_modes文件夹中创建了.htaccess文件
  5. 我将error_modes文件夹的权限设置为777可写。
  6. 我故意在footer.inc.php页面中写了<?php 'should see this error in log file' ?> 请注意我没有写信; 字符结尾。

尽管我的footer.inc.php页面中故意存在php语法错误 ,但没有创建php_error.log文件!

并且我看到应该footer.inc.php页面中打印了日志文件 string 此错误 因此,尽管存在语法错误,php仍能正常工作!

我还在下面添加了整个.htaccess代码。 (这是位于public_html下的那个)

仅供参考 :我没有访问php.ini权限,也没有任何预设的.log文件。 PHP版本是5.4。

你能纠正我吗? 谢谢。 最好的祝福。

将命令添加到public_html> .htaccess中以进行错误处理

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

error_modes中的代码> .htaccess

Order allow,deny
Deny from all

整个代码位于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

请尝试执行以下操作:

在.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

在php文件中

除了在您的php文件中编写的故意错误之外,您可以尝试执行以下操作:

    <?

    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


    ?>

和我一起工作很好)

希望能对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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