2012-04-30 71 views
1

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

下面的代码:

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()之前将其设置,但它启动了一个全新的会话。请帮忙!

+1

在使用Zend框架时,不要直接使用$ _SESSION变量(也不要使用php自己的会话函数),这是他们建议最多的事情之一。 Zend拥有自己的Session管理系统及其相应的类。 ...您应该为不读文档而投下一票:P – olanod

+0

不知道他们有一个完整的单独的类来处理会话。这是从初学者的书中学习,然后立即尝试制作真实世界的应用程序的不利之处。感谢您的领导! :) –

回答

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; 
    } 

P.S.如果你要去ZF ...去ZF! :)