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

标签 php .htaccess error-handling text-files error-logging

以“只有管理员可以看到警告、错误等”为目标的 PHP 错误处理;

我应用了以下步骤:

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

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

我看到应该在日志文件中看到这个错误 string印在我的footer.inc.php页。所以尽管存在语法错误,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


    ?>

和我一起工作很好)

希望对您有所帮助。

关于使用 .htaccess 处理 PHP 错误并写入 php_error.log 文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16374526/

相关文章:

php - 使用 cron : Could not open logfile, 运行 php 脚本这样的文件或目录

php - 如何在 OpenCart 中创建自定义的 SEO 友好 URL?

php - 引用 - 这个错误在 PHP 中意味着什么?

json - 离线 : Failure: package./home/denzilgupta/serverless-testing/.webpack/service/services/trader/package.json 中不存在 json

php - 找不到类(在 PHP 中)

php - Laravel Carbon 入门 + 本周末

apache - 如何在根 url(/- 无路径)中运行 FastCGI 脚本?

.htaccess - htaccess 文件中的数学运算符

android - 如何从Android WebView中的iframe捕获错误

javascript - 如果未选中复选框且输入字段为空,则禁用 jquery 中的按钮