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

问题描述 投票:0回答:2

我想提前说声抱歉,因为我的母语不是英语,但我会尽力解释我遇到的问题。我有一个基于多个步骤的注册表。我在处理会话中存储的一些数据时遇到问题。我将在下面解释导航流程:

  1. (第一步)用户自行注册并单击提交按钮。如果使用我当前模型中的方法在会话中成功提交数据,它将重定向到下一页(我当前类中的另一个方法)

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

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

当我检索会话中的数据时(我的模型中有一个方法可以做到这一点),我看不到我在上一页设置的电话号码,但我只能看到这个数据(电话号码)只有当我刷新我当前的页面。

我已经检查了所有数据流,但我不明白为什么我有这种数据行为,有人可以帮我吗?

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] => [email protected]
        [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 which 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 which 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
     }
php session
2个回答
0
投票

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

希望我有帮助


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(){
    }
}

谢谢你

© www.soinside.com 2019 - 2024. All rights reserved.