我在$_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()
之前对其进行设置,但它启动了一个全新的会话。请帮忙!
我不确定这是否会有所帮助,因为我不确定步骤 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;
}
附言如果你要去采埃孚...去采埃孚!:)