1

我在将变量存储在变量中时遇到问题$_SESSION。我正在使用 Zend 框架并构建一个 3 步应用程序表单。现在,当第一步完成后,我将数据存储在 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(),但它会启动一个全新的会话。请帮忙!

4

1 回答 1

1

我不确定这是否会有所帮助,因为我不确定在第 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如果你要去采埃孚……去采埃孚!:)

于 2012-04-30T04:43:12.227 回答