“会话已经开始……"Zend Framework 应用程序中的异常
我在尝试加载 Zend Framework 应用程序时收到此错误:
I get this error when trying to load a Zend Framework application:
致命错误:未捕获的异常带有消息的Zend_Session_Exception"'会话已经由session.auto-start 或 session_start()'在/www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Session.php:462
Fatal error: Uncaught exception 'Zend_Session_Exception' with message 'session has already been started by session.auto-start or session_start()' in /www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Session.php:462
堆栈跟踪:
#0/www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Session/Namespace.php(143):Zend_Session::start(true)
#0 /www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Session/Namespace.php(143): Zend_Session::start(true)
#1/www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Auth/Storage/Session.php(87):Zend_Session_Namespace->__construct('Zend_Auth')
#1 /www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Auth/Storage/Session.php(87): Zend_Session_Namespace->__construct('Zend_Auth')
#2/www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Auth.php(91):Zend_Auth_Storage_Session->__construct()
#2 /www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Auth.php(91): Zend_Auth_Storage_Session->__construct()
#3/www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Auth.php(141):Zend_Auth->getStorage()
#3 /www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Auth.php(141): Zend_Auth->getStorage()
#4/www/htdocs/w00a1ed7/autospin/redaktion/application/layouts/scripts/layout.phtml(31):Zend_Auth->hasIdentity()
#4 /www/htdocs/w00a1ed7/autospin/redaktion/application/layouts/scripts/layout.phtml(31): Zend_Auth->hasIdentity()
#5/www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/View.php(108):包括('/www/htdocs/w00...')
#5 /www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/View.php(108): include('/www/htdocs/w00...')
#6/www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/View/Abstract.php(831):Zend_View->_run('/www/htdocs/w00...')
#6 /www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/View/Abstract.php(831): Zend_View->_run('/www/htdocs/w00...')
#7/www/htdocs/w00a1ed 在/www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Session.php在线 462
#7 /www/htdocs/w00a1ed in /www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Session.php on line 462
我使用 Zend_Auth
并在我的本地服务器上运行良好,但在生产服务器上我收到了前面的错误,但不是每次都如此.
I Use Zend_Auth
and on my local server and it works well, but on a production server I get the preceding error, but not every time.
我已检查 .htaccess
文件中的 session.autostart
是否设置为 0
.
I have checked that session.autostart
is set to 0
in the .htaccess
file.
我该如何解决这个错误?
How do I fix this error?
感谢您的回答,但我不会在任何地方使用 session_start().仅与采埃孚合作.
Thank you for your Answer, but I do not user session_start() anywhere. Work only with ZF.
我只在共享服务器上遇到这个问题,在我的本地服务器脚本上运行完美.
I Have this Problem only on shared server, on my local server script works perfectly.
我在此代码中使用 INIT 函数:
I Use INIT Function with this code:
受保护的 $user;
protected $user;
public function init()
{
if(!Zend_Auth::getInstance()->hasIdentity())
{
$this->_redirect('auth/login');
}else
{
$this->user = Zend_Auth::getInstance()->getIdentity();
}
}
我已经尝试只在indexAction中设置tis代码,这样其他action就不用去破解Auth了...但是还是有问题.
I allready try to set tis code only in indexAction, so that other actions do not have to chack the Auth... but still have problems.
有没有办法在 Action 中设置不检查会话或类似这样的想法?
Ist there a way to set in an Action to do not check about session or somethink like this?
最好的问候
推荐答案
事情就是这样.Zend_Auth
尝试开始一个新的会话,因为 Zend_Session::start()
还没有被调用.
It's what it says it is. Zend_Auth
tries to start a new session, since Zend_Session::start()
has not yet been called.
问题是必须在会话开始之前调用 Zend_Session::start()
.但是,由于 session.autostart 是 0(顺便说一句,这是在 php.ini 中而不是 .htaccess 中),您可能已经编写了 session_start();
某处.您不能这样做,因为 ZF 希望完全控制会话,即您不应直接访问全局会话变量.
The problem is that Zend_Session::start()
has to be called before a session is started. But, since session.autostart is 0 (btw this is in php.ini not .htaccess), you have probably written session_start();
somewhere. You're not allowed to do that, since ZF wishes to have full control over sessions, i.e. you shouldn't access the global session variable directly.
要解决它,请在您的代码文件中搜索 session_start()
和其中之一
To solve it, search your code files for session_start()
and either
- 删除所有出现,但只有一个.要注意它是否已经启动,请设置
error_reporting(E_ALL|E_STRICT);
- 在所有地方用
Zend_Session::start();
替换它
如果你不能找到所有的出现,找到一个 session_start();这会困扰您的 Zend_Auth::getInstance()->hasIdentity()
并使用以下代码段快速解决问题
If you can't find all occurrences, find the one session_start(); that bothers your Zend_Auth::getInstance()->hasIdentity()
and solve the problem quick n' dirty with the following snippet
try {
Zend_Session::start();
} catch(Zend_Session_Exception $e) {
session_start();
}
如果您在整个应用程序中使用 ZF,我会选择 2)
If you're using ZF in your whole application, I would go with 2)
相关文章