dongwo1914 2017-03-10 19:14
浏览 13
已采纳

我看不到我之前在会话PHP中存储的一些数据

Hello I want to say sorry in advance because I am not a english native speaker but I will do my best explaining the issue that I have , I have a registration form based in multiple steps, I have a issue dealing with some data which is stored in the session, I'll explain the navigation flow below:

1.- (First step) The user register himself and click the submit button, If the submit is success set the data in the session using a method in my current model and redirect to the next page (another method in my current class)

2.- (Second step) The user set his address, e-mail and phonen number (which is optional) and click the submit button If the submit is success set the data in the session using a method in my current model and redirect the user to the next page. (Other method)

3.- (The third step) THE ISSUE IS HERE At this point I printed the all the data in the session variable of course the phone fild is empty because I did not fill it in the previous page, when I back to the previos page (User adress) in order to set a phone number and press the submit button, I store the post data in the session again (overwrite only the array in the session which belongs to this step), after press the submit, my app redirecme to this step again (Third step),

when I retrieve the data in the session (I have a method in my model to do this), I can not see the phone number that I set the prevevious page :S , but I can see the this data (phone number) Only when I refresh my current page.

I already checked all the data flow but I cannot understand why I have this data behavior, does someone can give me a hand?

Array(
[usuario] => Array  // Data stored in the First Step
    (
        [usr_nombre] => Name
        [usr_paterno] => Middle Name
        [usr_materno] => Surename
        [usr_curp] => curp
        [usr_rfc] => rfc
        [usr_email] => fake@mail.com
        [usr_acepta] => 1
    )

[direccion] => Array // Data stored in the second step
    (
        [usr_tel] =>              <-- Phone number is empty, which is ok because is an optional field and it was not set in the previous screen
        [usr_dir] => direccion
        [usr_colonia] => colonia
        [usr_codigo] => 55130
        [usr_pais] => 147
        [usr_region] => 2410
        [usr_ciudad] => municipio
    )

[cursos] => Array  // Third step
    (
        [crs_1] => 1
    ))

My code with some explanation

    if (!isset($_SESSION)){
    session_cache_limiter('private_no_expire');
    session_start();
    }
     class Registro extends Controllers
     {
     /**  First Step
       Show user form
     */
     public function usuario(){
       $mdlReg = Models::load('Registro'); // Model
       $data = $mdlReg->getSession('all');  // Retrieve all the data stored in the session
       if($this->post && $this->post['add'])  // The add key comes fron a hidden field in my form
       {    
         $res = $mdlReg->verificar_datos($this->post['usuario'], 'usuario');  // Validate all  data types and empty fields (If everything it's ok this will return true)
         if($res['status'] == TRUE){
           $mdlReg->setSession('usuario', $this->post['usuario']);  //Set the  array data  called usuario() in to the session using a method in  the model
           ClassGoUrl::go($this -> getUrl('ac').'direccion');       //Redirect to the next step (direccion method)
           exit;
         }
       }
       /*
         Check if the data of this form exist in the session, if do not exist then fill the values with 
         the post values (In case the submit validation fails in order to avoid the user to fill all the form again)
       */
       $reg_direccion = $mdlReg->getSession('direccion');
       $reg_direccion = $reg_direccion?$reg_direccion:$this->post['direccion'];
       $form = new ClassForm('usuario');
       $form->values($reg_usuario);        // This method  Fill the form field in the current step form
        // ..................... My form here 
     }  
     /**   Second Step
       Show address & contact form
     */
     public function direccion(){
       $mdlReg = Models::load('Registro');
       $data = $mdlReg->getSession('usuario');
       // If the data wich come from the first Step (in the session) does not exist redirect to the (First Step)
       if(empty($data)) ClassGoUrl::go($this -> getUrl('ac').'usuario');
       if($this->post && $this->post['update']) // The update key  comes fron a hidden field in my form
       {    
          $res = $mdlReg->verificar_datos($this->post['direccion'], 'direccion'); // Validate all  data types and empty fields (If everything it's ok this will returntrue)
         if($res['status'] == TRUE){
           $mdlReg->setSession('direccion', $this->post['direccion']); // add the array 'direcction' to the session
           ClassGoUrl::go($this -> getUrl('ac').'curso');  // Redirect to the next step (curso method)
           exit;
         }
       }
       /*
         Check if the data of this form exist in the session, if do not exist then fill the values with 
         the post values (In case the submit validation fails in order to avoid the user to fill all the form again)
       */
       $reg_direccion = $mdlReg->getSession('direccion'); // Gte  the data of teh curren step from the session
       $reg_direccion = $reg_direccion?$reg_direccion:$this->post['direccion']; // Set the values to the form elements (from session or from the post)
       $form = new ClassForm('completar');
       $form->values($reg_direccion);  // This method  Fill the form field in the current step form
       //.......... My form here
     }
     public function curso(){    
       $mdlReg = Models::load('Registro');
       $data = $mdlReg->getSession('direccion');
       print_r($_SESSION);
       /* !!!! THE  ISSUE IS HERE !!!!!!!!!
         After fill the previus form (direccion method) I leave the phone number empty (its an optional fild), at this point I print all the data in my 
         session array everithing its ok, but when I go back to the previos step, in order to fill  my phone number (some people can do that)
          I set the phone numbrer and click the submit in order to stored the data in the session (in the direccion method whith is going to redirectme to this
          method again)
          when I see the data in the session array , the phone number is not there until I refresh the page (in this method)
       */
       // If the data wich come from the Second Step (in the session) does not exist redirect to the (second step)
       if(empty($data)) ClassGoUrl::go($this -> getUrl('ac').'direccion');    
       if($this->post && $this->post['addUserCurso'] == 1) // The addUserCurso key  comes fron a hidden field in my form
       {    
         $res = $mdlReg->verificar_datos($this->post['cursos'], 'cursos');
         if($res['status'] == TRUE){
           $mdlReg->setSession('cursos', $this->post['cursos']); //Set the  array data  called 'curso' in to the session using a method in  the model
           ClassGoUrl::go($this -> getUrl('ac').'resumen');  // Go to next Step
           exit;
         }
       };  
       $form = new ClassForm('Curso');
       // .......... My form here
     }
  • 写回答

2条回答 默认 最新

  • doulianqi3870 2017-03-10 22:27
    关注

    After reading and trying many stuff, I finally fixed this issue using session_start(); on the constructor of my class.

    // I was calling the session_start() method here
    class Test { 
        public function __construct(){
          session_start();
        }
    
        public function method1(){
        }
    
        public function method2(){
        }
    
        public function method3(){
        }
    }
    

    Thank you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?