php “会话已经开始......” Zend Framework 应用程序中的异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2418124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:23:38  来源:igfitidea点击:

"session has already been started...." exception in Zend Framework application

phpzend-frameworksession.htaccesszend-auth

提问by Fribu - Smart Solutions

I get this error when trying to load a Zend Framework application:

尝试加载 Zend Framework 应用程序时出现此错误:

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

Stack trace:

#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')

#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()

#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): include('/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 in /www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Session.php on line 462

致命错误:/www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Session.php:462 中的消息“会话已由 session.auto-start 或 session_start() 启动”的未捕获异常“Zend_Session_Exception”

堆栈跟踪:

#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')

#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()

#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): include('/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 in /www/htdocs/w00a1ed7/autospin/redaktion/library/Zend/Session.php 第 462 行

I Use Zend_Authand on my local server and it works well, but on a production server I get the preceding error, but not every time.

Zend_Auth在本地服务器上使用和 并且它运行良好,但是在生产服务器上我得到了前面的错误,但不是每次都这样。

I have checked that session.autostartis set to 0in the .htaccessfile.

我已经检查过在文件中session.autostart设置为。0.htaccess

How do I fix this error?

我该如何解决这个错误?



Thank you for your Answer, but I do not user session_start() anywhere. Work only with ZF.

感谢您的回答,但我不会在任何地方使用 session_start()。仅与采埃孚合作。

I Have this Problem only on shared server, on my local server script works perfectly.

我只在共享服务器上遇到这个问题,在我的本地服务器脚本上运行完美。

I Use INIT Function with this code:

我将 INIT 函数与此代码一起使用:

protected $user;

受保护的 $user;

public function init()
{   
    if(!Zend_Auth::getInstance()->hasIdentity())
    {
        $this->_redirect('auth/login');
    }else
    {
        $this->user = Zend_Auth::getInstance()->getIdentity();
    }
}

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.

我已经尝试仅在 indexAction 中设置 tis 代码,以便其他操作不必破解 Auth...但仍然存在问题。

Ist there a way to set in an Action to do not check about session or somethink like this?

有没有办法在 Action 中设置不检查会话或这样的想法?

Beste regards

最好的问候

回答by Del

Before this drives you mad, there's probably nothing wrong with your code!

在这让您发疯之前,您的代码可能没有任何问题!

Check your application.ini for the session save path, for me it is APPLICATION_PATH '/session'

检查您的 application.ini 以获取会话保存路径,对我来说它是 APPLICATION_PATH '/session'

Now check you have the correct permissions! If not then cd into the application folder and type

现在检查您是否拥有正确的权限!如果没有,则 cd 进入应用程序文件夹并键入

sudo chmod 777 session
sudo chown -R [usernamehere] session
sudo chgrp -R [usernamehere] session

Job Done!

任务完成!

回答by chelmertz

It's what it says it is. Zend_Authtries to start a new session, since Zend_Session::start()has not yet been called.

这就是它所说的。Zend_Auth尝试开始一个新的会话,因为Zend_Session::start()还没有被调用。

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

问题是Zend_Session::start()必须在会话开始之前调用。但是,由于 session.autostart 是 0(顺便说一句,这是在php.ini而不是.htaccess 中),您可能已经在session_start();某处写过。您不能这样做,因为 ZF 希望完全控制会话,即您不应直接访问全局会话变量。

To solve it, search your code files for session_start()and either

要解决它,请在您的代码文件中搜索session_start()

  1. remove all occurences but one. To notice if it's already been started, set error_reporting(E_ALL|E_STRICT);
  2. replace it with Zend_Session::start();at all places
  1. 删除所有出现,但一个。要注意它是否已经启动,请设置error_reporting(E_ALL|E_STRICT);
  2. Zend_Session::start();在所有地方替换它

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

如果你不能找到所有的出现,找到一个 session_start(); Zend_Auth::getInstance()->hasIdentity()使用以下代码段困扰您并快速解决问题

try {
    Zend_Session::start();
} catch(Zend_Session_Exception $e) {
    session_start();
}

If you're using ZF in your whole application, I would go with 2)

如果您在整个应用程序中使用 ZF,我会选择 2)

回答by Thomas

I had the same error. On local machine, everything worked fine. On server not. My solution was to put Zend_Session::start();in the index.php before running bootstrap. So that it looks like this:

我有同样的错误。在本地机器上,一切正常。在服务器上没有。我的解决方案是Zend_Session::start();在运行引导程序之前放入index.php。所以它看起来像这样:

<?php
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';  

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV, 
    APPLICATION_PATH . '/configs/application.ini'
);

error_reporting(E_ALL);
ini_set("display_errors", 1);

Zend_Session::start();

$application->bootstrap()->run();

回答by Tayyab Khan

There are 3 major reasons that produce this issue:

产生此问题的主要原因有 3 个:

  1. session.auto_start should be set to 0 or off. You can check it by placing phpinfo(); in any file and try to access it in browser, then search auto_start is it 0 or off. If not then set it off or 0.
  2. Check session path session.save_path in configuration at server. If with default configuration it's displaying error 'session has already been started by session.auto-start or session_start()', set it to '/temp'
  3. May be you have used session_start() in your code before zend session initialization.
  1. session.auto_start 应设置为 0 或关闭。您可以通过放置 phpinfo(); 来检查它。在任何文件中并尝试在浏览器中访问它,然后搜索 auto_start 是 0 还是关闭。如果没有,则将其设置为关闭或 0。
  2. 在服务器的配置中检查会话路径 session.save_path。如果使用默认配置显示错误“会话已由 session.auto-start 或 session_start() 启动”,请将其设置为“/temp”
  3. 可能是您在 zend 会话初始化之前在代码中使用了 session_start()。

In most cases the 2nd option is the reason.

在大多数情况下,第二个选项是原因。

回答by Kiril Kostadi

If you are developing applications with the RPCLlibrary (RADPHP) and you are getting this error:

如果您正在使用RPCL库 ( RADPHP)开发应用程序并且您收到此错误:

Application raised an exception class Zend_Session_Exception with message 'session has already been started by session.auto-start or session_start()',

应用程序引发异常类 Zend_Session_Exception 并带有消息“会话已由 session.auto-start 或 session_start() 启动”,

then here is my solution.

那么这是我的解决方案。

You will be surprised how simple it is. Simple include the line

你会惊讶它是多么简单。简单的包含行

require_once("zcommon.inc.php");

require_once("zcommon.inc.php");

just after the opening PHP tag in the file containing ZAuthcomponent – usually this is the file with a DataModuleform. Of course make sure the file zcommon.inc.phpis in your path. This will ensure that the Zend session will start first instead of the RPCL session.

就在包含ZAuth组件的文件中打开 PHP 标记之后——通常这是带有DataModule表单的文件 。当然,请确保文件zcommon.inc.php在您的路径中。这将确保 Zend 会话将首先启动,而不是 RPCL 会话。

Also make sure the name of the php files in your application correspond to the name of the containing classes.

还要确保应用程序中 php 文件的名称与包含类的名称相对应。

回答by ACNB

I'd like to draw your attention to the garbage collection problem, solved here Issues with PHP 5.3 and sessions folderor http://somethingemporium.com/2007/06/obscure-error-with-php5-on-debian-ubuntu-session-phpini-garbage.

我想提请您注意垃圾收集问题,这里解决了 PHP 5.3 和会话文件夹的问题http://somethingemporium.com/2007/06/obscure-error-with-php5-on-debian-ubuntu-会话-phpini-垃圾

After fixing the 'session has already been started' I encountered the GC bug. I suspect that the GC bug might be the root cause for the session bug at least in some instances. So far I have not had enough time to investigate thoroughly, but please comment if GC and session bug are related in your case as well.

修复“会话已经开始”后,我遇到了 GC 错误。我怀疑至少在某些情况下,GC 错误可能是会话错误的根本原因。到目前为止,我还没有足够的时间进行彻底调查,但请评论 GC 和会话错误是否也与您的情况相关。

回答by pringlized

For those moving from one server to another. Another issue can be user that apache runs under. I was running a different user on my old box that was set on the new one. I used configs from my old httpd.conf, and forgot to update the permissions on /var/lib/php/session to reflect the different user.

对于那些从一台服务器移动到另一台服务器的人。另一个问题可能是 apache 在其下运行的用户。我在旧机器上运行了一个不同的用户,而这个用户是在新机器上设置的。我使用旧 httpd.conf 中的配置,但忘记更新 /var/lib/php/session 上的权限以反映不同的用户。

To test, I changed the perms to 777. Everything worked fine, error was gone:

为了测试,我将 perms 更改为 777。一切正常,错误消失了:

# cd /var/lib/php
# chmod 0777 session

So I returned the perms and changed the group. Of course change newApacheUser to the user account you are running httpd on IF NOT apache.

所以我退回了烫发并更改了组。当然,如果不是 apache,请将 newApacheUser 更改为您在 httpd 上运行的用户帐户。

# chmod 0770 session
# chown root:newApacheUser session

Something to check out if your still having this issue:

如果您仍然遇到此问题,请检查一下:

Fatal error: Uncaught exception 'Zend_Session_Exception' with message 'session has already been started by session.auto-start or session_start()'

致命错误:未捕获的异常 'Zend_Session_Exception' 带有消息 'session has been started by session.auto-start or session_start()'

回答by Felix

had the same error. it occured only if two instances of the same session were used at the same time (e.g. two browser instances were loading at the same time). This is a result of php not being able to handle two open sessions with the same id at the same time.

有同样的错误。仅当同时使用同一会话的两个实例时才会发生这种情况(例如,同时加载两个浏览器实例)。这是 php 无法同时处理两个具有相同 id 的打开会话的结果。

回答by ButtToffee

In case this of any use, I cleared this error by taking out session related lines from my application/config/application.ini

如果有任何用途,我通过从 application/config/application.ini 中取出与会话相关的行来清除此错误

;resources.session.save_path = APPLICATION_PATH "/../data/session"
;resources.session.use_only_cookies = true
;resources.session.remember_me_seconds = 3600

Thanks to chelmertzfor the insight into the cause of the problem.

感谢chelmertz洞察问题的原因。