【问题标题】:Uncaught exception 'Zend_Session_Exception' with message带有消息的未捕获异常“Zend_Session_Exception”
【发布时间】:2014-08-08 03:52:17
【问题描述】:

当我在 Zend Framework 中运行我的 Cron 作业时,它会出现以下错误


致命错误:未捕获的异常“Zend_Session_Exception”带有消息“必须在任何输出发送到浏览器之前启动会话;输出开始于 /home/ZendFramework/library/Zend/Session.php:456 中的 /0' 堆栈跟踪: #0 /home/ZendFramework/library/Zend/Session/Namespace.php(143): Zend_Session::start(true) #1 /home/atypqapp/public_html/library/Plugins/AccessCheck.php(17): Zend_Session_Namespace->__construct('licence_error') #2 /home/atypqapp/public_html/application/modules/backend/Bootstrap.php(16): Plugins_AccessCheck->__construct(Object(Backend_Model_Libraryacl), Object(Zend_Auth)) #3 /home/ZendFramework/library/Zend/Application/Bootstrap/BootstrapAbstract.php(679): Backend_Bootstrap->_initAutoload() #4 /home/ZendFramework/library/Zend/Application/Bootstrap/BootstrapAbstract.php(632): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource('autoload') #5 /home/ZendFramework/library/Zend/Application/Bootstrap/BootstrapAbstract.php(596): Zend_Application_Bootstrap_BootstrapAbstract->_bootstrap(NULL) #6 在 /home/ZendFramework/library/Zend/Session.php456

这是我的库>插件 AccessCheck.php

class Plugins_AccessCheck extends Zend_Controller_Plugin_Abstract {

    private $_acl = null;
    private $_auth = null;

    public function __construct(Zend_Acl $acl, Zend_Auth $auth) {

        $this->_acl = $acl;
        $this->_auth = $auth;

        //*******************************
        // Checking License
        //******************************

        $licence_error = new Zend_Session_Namespace('licence_error');

        if (!$licence_error->active === NULL) {
            throw new Exception('Licence Error', 404);
        }

        // 
    }

    public function preDispatch(Zend_Controller_Request_Abstract $request) {


        parent::preDispatch($request);
        //  echo 'PRE DISPATCH';



        $resoruce = $this->_request->getControllerName();
        $action = $this->_request->getActionName();



        $identity = $this->_auth->getStorage()->read();

        //var_dump($identity);

        if (isset($identity)) {
            if (isset($identity->userID)) {
                //Load Adapter
                //  Zend_Registry::get("db");
                $previlages_db = new Backend_Model_Usersprofile();
                //get Role ID and find out the name of role

                $previlages_db_results = $previlages_db->loadProfileIDsSpecificUser($identity->userID);
                //  var_dump($identity->userID);

                $roles = array();

                foreach ($previlages_db_results as $value) {
                    array_push($roles, $value['profile_name']);

                    //var_dump($this->_acl->isAllowed($value['profile_name'], $resoruce, $action));
                    if (!$this->_acl->isAllowed($value['profile_name'], $resoruce, $action)) {
                        $request->setModuleName('public');
                        $request->setControllerName('unauthorized');
                        $request->setActionName('index');
                    } else {
                        //If one profile is OK,Give Permission To Access Particular Resources
                        break;
                    }
                }
                // var_dump($roles);
                // echo 'Allowed or not *******************************';
            }
        } else if (!defined('_CRONJOB_') || _CRONJOB_ == false) {
             //Load Adapter
                //  Zend_Registry::get("db");
                $previlages_db = new Backend_Model_Usersprofile();
                //get Role ID and find out the name of role

                $previlages_db_results = $previlages_db->loadProfileIDsSpecificUser($identity->userID);


                $roles = array();

            foreach ($previlages_db_results as $value) {
                    array_push($roles, $value['profile_name']);                   
                    if (!$this->_acl->isAllowed($value['profile_name'], $resoruce, $action)) {
                        $request->setModuleName('public');
                        $request->setControllerName('unauthorized');
                        $request->setActionName('index');
                    } else {
                        //If one profile is OK,Give Permission To Access Particular Resources
                        break;
                    }
            }

        }
        if ($this->_request->isXmlHttpRequest()) {
            if (!$this->_acl->isAllowed($value['profile_name'], $resoruce, $action)) {
                $request->setModuleName('public');
                $request->setControllerName('unauthorized');
                $request->setActionName('index');
            }
        }
    }

}

【问题讨论】:

  • Session must be started before any output has been sent to the browser”。不能说得更清楚了;在开始会话之前不要输出内容。
  • 那么,我现在该怎么办??
  • 您应该解决问题,如错误消息中所述。你不明白什么?有什么问题?
  • 先生,我该怎么做才能防止这个错误?我没有回应任何人......所以......该消息仍然显示......在唯一的cron日志上!!!
  • 没有理由这么正式;我的名字是斯维里。要修复它,您必须在开始会话之前删除 all 输出。接受this question 的答案应该可以帮助您找到输出的来源。

标签: php session zend-framework


【解决方案1】:

您需要在应用程序中更早地启动会话。从您的堆栈跟踪中,我可以看到您正在调用扩展 BootstrapAbstract 的后端引导文件。要修复此错误,您可以在该文件中初始化会话。请注意,引导文件中的任何 _init 方法都会自动调用。

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initSession()
    {
        // do any extra session config here
        Zend_Session::start();
    }
}

【讨论】:

    【解决方案2】:

    尝试在Plugins_AccessCheck 类中的routeStartup 函数中移动有关会话的代码,如下所示:

    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {        
        //*******************************
        // Checking License
        //******************************
    
        $licence_error = new Zend_Session_Namespace('licence_error');
    
        if (!$licence_error->active === NULL) {
            throw new Exception('Licence Error', 404);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-14
      • 1970-01-01
      • 2013-02-01
      • 2016-02-27
      • 1970-01-01
      • 2012-07-08
      • 2012-12-03
      • 2014-11-15
      • 2014-08-09
      相关资源
      最近更新 更多