【问题标题】:.htaccess display_startup_errors setting depends on php script config file.htaccess display_startup_errors 设置取决于 php 脚本配置文件
【发布时间】:2013-03-17 00:28:21
【问题描述】:

我正在创建自己的框架作为学习过程。有一个配置文件,人们可以在其中设置框架是否处于开发模式。

<?PHP
$project[security][dev_mode] = true;
?>

Display_startup_errors 在 .htaccess 中定义,以指示是否应显示语法错误。如果用户不需要弄乱 .htaccess 文件,我希望它可以“调整”到配置文件中的设置。有人知道if如何 有可能以某种方式让 .htaccess 检查 php 文件的内容并采取相应措施吗?

也欢迎以 .htaccess 以外的其他方式设置 display_startup_errors 的解决方案;-)。

非常感谢!

【问题讨论】:

  • display_startup_errors 将成为人们想要设置的东西。我不希望 php 框架尝试控制 http 服务器。话虽如此,我不知道有一种简单的方法可以完成你所说的。 htaccess 只是一个 iirc 指令文件。
  • 你可能是对的,但这个框架不会大规模分发;-)。谢谢。
  • 为什么使用.htaccess来显示错误而不是php的ini_se()配置?
  • @kaii 我的理解是不能使用 ini_set() 设置语法错误...否则,是否有可能以某种方式推翻使用 .htaccess 在 php 脚本中设置的 display_startup_errors 设置?跨度>
  • @dirk 可以使用 php 显示语法错误.. :)

标签: php .htaccess error-handling


【解决方案1】:

试试

<?php
$iDevMode = ( $project['security']['dev_mode'] ) ? 1 : 0;

ini_set('display_errors', $iDevMode);
?>

根据定义进行切换。这是一个丑陋的三元运算(您可以将其转换为 if 语句进行练习),并且需要在程序的早期处理。

还请注意,PHP 会抛出一个通知,因为我没有将关联数组引用封装在引号中,就像我上面所说的那样。

【讨论】:

  • 脚本一发现语法错误就会终止(在处理任何代码之前),我知道 display_startup_errors 不能使用 ini_set 设置...
【解决方案2】:

使用 .htaccess 处理错误的另一种方法是创建一个可配置的 php 文件,可以像其他框架一样在开发、生产和测试阶段进行设置。

 * You can load different configurations depending on your
 * current environment. Setting the environment also influences
 * things like logging and error reporting.
 *
 * This can be set to anything, but default usage is:
 *
 *     development
 *     testing
 *     production
 *
 * NOTE: If you change these, also change the error_reporting() code below
 *
 */
    define('ENVIRONMENT', 'development');
/*
 *---------------------------------------------------------------
 * ERROR REPORTING
 *---------------------------------------------------------------
 *
 * Different environments will require different levels of error reporting.
 * By default development will show errors but testing and live will hide them.
 */

if (defined('ENVIRONMENT'))
{
    switch (ENVIRONMENT)
    {
        case 'development':
            error_reporting(E_ALL);
        break;

        case 'testing':
        case 'production':
            error_reporting(0);
        break;

        default:
            exit('The application environment is not set correctly.');
    }
}

或者手动尝试使用ini_set()将错误处理的配置正确设置为on

// change settings for error handler to show errors
// $this setup is used for checking errors for development to be shown.... 
ini_set('display_errors', 1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 2011-01-09
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多