繁体   English   中英

Zend框架的会话持久性

[英]Session persistence with Zend framework

我在将变量存储在$_SESSION变量中遇到问题。 我正在使用Zend框架并构建一个三步申请表。 现在,第一步完成后,我将数据存储在MySQL数据库中,并将返回的插入ID存储在会话变量中。 然后,我将页面转发到另一个控制器(步骤2)。 当我转发请求时,一切正常,我可以从会话变量中读取ID。 但是,当我提交第二个表单(与操作具有与步骤2相同的控制器)时,会话将丢失。 我尝试var_dump它,它返回NULL

这是代码:

public function organizationAction()
{

    $this->view->vals="";
    $form=$this->getOrganizationForm();
    $this->aplid=$_SESSION['appid'];
    var_dump($_SESSION);
    $firsttime=$this->getRequest()->getParam('firsttime',0);

    //if(null==$this->aplid) $this->_forward('index','index');
    if ($this->getRequest()->isPost() && $firsttime==0) {
        if (!$form->isValid($_POST)) {
            // Failed validation; redisplay form
            $this->view->form = $form;
            return false;
        }
        var_dump($_SESSION);
        $values = $form->getValues();
        $db=new Util_Database();

        if($db->insertOrganization($values,$this->aplid))
            $this->_forward('final');
        else echo "An error occured while attempting to submit data. Please try agian";

    }


    $this->view->form=$form;
}

这里有什么问题? 我尝试将session_id存储在表单中,然后在session_start()之前进行设置,但它会启动一个全新的会话。 请帮忙!

我不确定这是否会有所帮助,因为我不确定在步骤2中是否还会发生其他事情。
您可能会无意中覆盖了会话数据。 这是我想出的可能有助于提出一些想法的方法。

public function organizationAction() {

        $this->view->vals = "";
        $form = $this->getOrganizationForm();
        $db = new Util_Database();
        //This will only submit the form if the is post and firsttime == 0
        if ($this->getRequest()->isPost() && $this->getRequest()->getPost('firsttime') == 0) {
            //if form is valid set session and save to db
            if ($form->isValid($this->getRequest()->getPost())) {
                //We only want to initialize the session this time, if we do it
                //on the next pass we may overwrite the information.
                //initialize session namespace
                $session = new Zend_Session_Namespace('application');
                //get values from form, validated and filtered
                $values = $form->getValues();
                //assign form value appid to session namespace
                $session->appid = $form->getValue('appid');
                //assign session variable appid to property aplid
                $this->aplid = $session->appid;
                if ($db->insertOrganization($values, $this->aplid))
                    $this->_forward('final');
                else
                    echo "An error occured while attempting to submit data. Please try agian";
            } else {
                //if form is not vaild populate form for resubmission
                //validation errors will display of form page
                $form->populate($this->getRequest()->getPost());
            }
        }
        //if not post display form
        $this->view->form = $form;
    }

PS:如果您要去ZF ...去ZF! :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM