2

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

我应用了以下步骤:

  1. 我从我的删除error_reporting(-1);命令index.php
  2. 我将下面的行添加到我的文件夹.htaccesspublic_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页面上写了。请注意,我最后没有写;字符。

尽管我的页面中有故意的php 语法错误,但没有创建任何文件!footer.inc.phpphp_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
4

1 回答 1

8

请尝试执行以下操作:

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


    ?>

和我一起工作很好)

希望它会有所帮助。

于 2013-05-04T14:22:49.293 回答