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

时间:2017-03-10 19:14:12

标签: php session

你好,我想事先说对不起,因为我不是英语母语人士,但我会尽力解释我的问题,我有一个基于多个步骤的注册表,我有一个处理一些数据的问题这将存储在会话中,我将解释下面的导航流程:

1.-(第一步)用户注册并单击提交按钮,如果提交成功,则使用当前模型中的方法设置会话中的数据并重定向到下一页(当前的另一种方法)类)

2.-(第二步)用户设置他的地址,电子邮件和电话号码(可选)并单击提交按钮 如果提交成功,则使用当前模型中的方法在会话中设置数据,并将用户重定向到下一页。 (其他方法)

3.-(第三步)问题在这里 此时我打印了会话变量中的所有数据当然电话fild是空的,因为我没有填写它在上一页,当我回到previos页面(用户地址)以设置电话号码 并按下提交按钮,我将帖子数据再次存储在会话中(仅覆盖属于此步骤的会话中的数组),按下提交后,我的应用程序再次重定向到此步骤(第三步),

当我在会话中检索数据时(我的模型中有一个方法可以执行此操作),我看不到设置预先显示页面的电话号码:S,但我可以看到此数据(电话号码) )只有当我刷新当前 页。

我已经检查了所有数据流但我无法理解为什么我有这种数据行为,有人可以帮我一把吗?

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
    ))

我的代码有一些解释

    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 个答案:

答案 0 :(得分:0)

对于将使用来自php会话的数据的所有页面使用“session_start()”

我希望我有所帮助

答案 1 :(得分:0)

在阅读并尝试了很多东西后,我终于使用session_start()修复了这个问题。在我班上的构造函数。

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

    public function method1(){
    }

    public function method2(){
    }

    public function method3(){
    }
}

谢谢