【问题标题】:Notice using PEAR Auth注意使用 PEAR Auth
【发布时间】:2013-02-06 03:23:56
【问题描述】:

我正在尝试使用 PEAR Auth 进行 php 站点身份验证。我遵循了官方文档中的示例,但我无法摆脱很多像这样的通知警报:

Notice: Constant DB_OK already defined in /usr/share/php/DB.php on line 47
Call Stack:
0.0005 647400 1. {main}() /var/www/concursosRep/admin/index.php:0
0.0751 7100160 2. include('/var/www/concursosRep/admin/loginbeta.php') /var/www/concursosRep/admin/index.php:60
0.0788 7448160 3. Auth->start() /var/www/concursosRep/admin/loginbeta.php:114
0.0790 7448528 4. Auth->login() /usr/share/php/Auth.php:528
0.0790 7448608 5. Auth->_loadStorage() /usr/share/php/Auth.php:546
0.0790 7448608 6. Auth->_factory() /usr/share/php/Auth.php:445
0.0809 7681728 7. include_once('/usr/share/php/Auth/Container/DB.php') /usr/share/php/Auth.php:468
0.0839 8066384 8. require_once('/usr/share/php/DB.php') /usr/share/php/Auth/Container/DB.php:32
0.0869 8374552 9. define() /usr/share/php/DB.php:47

我知道这意味着该库以某种方式被多次包含,但我不知道如何修复它。在我的 php.ini 中,我在 include_path 中有这个:

include_path    .:/usr/share/php:/usr/share/php/libzend-framework-php

我首先认为问题是 Zend 在某个地方加载了 pear auth 的类,所以我将 include_path 更改为:.:/usr/share/php 但我遇到了同样的问题。

这是我的使用方法:

require_once ('Auth.php');//Pear Auth

   $dns = 'mysql://'.USER.':'.Util::decodePass(PASSWORD).'@'.SERVER.'/'.DBNAME;

  $options = array(
   'dsn' => $dns,
   'table' => 'usuario',
   'usernamecol' => 'login',
   'passwordcol' => 'password',
   'cryptType' => 'md5', //'sha1'
   'db_fields' => '*'
   );

  // Create the Auth object:
  $auth = new Auth('DB', $options, 'show_login_form');

  // Start the authorization:
  $auth->start();

  // Confirm authorization:
  if ($auth->checkAuth()) {
     //Authorized
          echo(javaScriptRedirect(true,$js));             

   } else { // Unauthorized.         
     echo(javaScriptRedirect(false,$js));

   }

我试图在我的系统中找到两个文件DB.php;这是我得到的:

 # sudo find -name DB.php -print
 ./usr/share/php/DB.php
 ./usr/share/php/Auth/Container/DB.php 

我试图找到我的脚本中包含的重复文件,这是我得到的:

#var_dump(get_included_files());
string(23) "/usr/share/php/Auth.php" [30] => string(36) "/usr/share/php/Auth/Container/DB.php" [31] => string(33) "/usr/share/php/Auth/Container.php" [32] => string(21) "/usr/share/php/DB.php" [33] => string(23) "/usr/share/php/PEAR.php" [34] => string(24) "/usr/share/php/PEAR5.php" [35] => string(27) "/usr/share/php/DB/mysql.php" [36] => string(28) "/usr/share/php/DB/common.php" }

希望有人可以帮助找出问题所在。 问候。

【问题讨论】:

  • 我试图在我的系统中找到两个文件 DB.php 这是我得到的: sudo find -name DB.php -print ./usr/share/php/DB.php ./usr/分享/php/Auth/Container/DB.php
  • 你的代码是否定义了DB_OK
  • 感谢你们的帮助。 @cweiske我没有在我的代码中定义那个常量。我只粘贴了一个通知,但是所有常量都有相同的警告通知,所以这让我觉得enterie PEAR被加载了不止一次。我从命令行安装了 PEAR,这可能很重要。

标签: php authentication pear


【解决方案1】:

注意1:Pear DB 是一个已弃用的库,因此您应该将 Auth 配置为使用 MDB2!

note2:这是一个通知,因此您的代码可能运行良好。

根据您提供的信息,很难判断 DB_OK 常量之前定义的位置。要做到这一点,需要完整的代码。

要调试这样的错误,您可以学习使用 XDEBUG 并逐步运行代码。 或者,如果您不想像专业人士那样学习和做到这一点,这里有一些关于如何找到它的丑陋想法:

把它放在代码的最开始:

declare(ticks=1);
function my_tick_function()
{
    if (defined('DB_OK'))
    {
        echo 'DB_OK defined for the first time as ' . DB_OK;
        var_dump(debug_backtrace());
        unregister_tick_function('my_tick_function');
    }
}
// using a function as the callback
register_tick_function('my_tick_function', true);

(我没有运行它,只是一个想法)

http://php.net/manual/en/function.register-tick-function.php

【讨论】:

    【解决方案2】:

    我认为DB.php 不会被加载两次,因为那样你会得到一个关于已经定义的类的致命错误——不仅仅是一个常量。

    如果我是你,我会 grep DB_OKdefine 通话的代码。要将选择限制为某些文件,请添加一个

    var_dump(get_included_files());
    

    在第 47 行的 /usr/share/php/DB.php 中。它会显示已包含哪些文件,因此 define() 调用需要在其中。


    另一种方法是使用xdebug's function traces 记录define() 的调用位置。如果您安装了 xdebug,这可能是最简单的解决方案。

    【讨论】:

    • 在调试时使用 get_included_files() 解决了我的另一个问题,+1!
    猜你喜欢
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多