Codeigniter会话->用户数据(当前)为null

时间:2019-01-28 16:37:53

标签: php codeigniter

我在控制器功能step2内创建会话curs。视图的表单操作转到该函数,但会话为空。

控制器步骤2:

public function step2($id=null){
    $this->load->library('session');
    $this->load->model('model_cursos');

    if(!$this->input->post()){

        if($id == null){
            redirect("Cursos/administrar");
        }

        $this->session->set_userdata('curs',$id);

        var_dump($this->session->userdata('curs'));// is not null

    }else{
        var_dump($this->session->all_userdata()); // is null
        exit;


        /*$titol = $_POST['titol'];
        $data = $_POST['data'];
        $horesT = $_POST['horesT'];
        $horesD = $_POST['horesD']; */

        //var_dump($titol,$data,$horesD,$horesT);

        //$this->model_cursos->afegirDades($id,$titol,$data,$horesT,$horesD);
        //$this->model_cursos->crearSessions($id,$data,$horesT,$horesD);
    }
    $this->load->view('step2');

}

查看步骤2:

    <form id="step2" method="post" action="<?php echo site_url('Cursos/step2'); ?>">

        <label>Titol</label><br>
        <input type="text" name="titol" id="titol"><br><br>
        <label>Data inici</label><br>
        <input type="date" name="data" id="data"><br><br>
        <label>Hores totals</label><br>
        <input type="number" name="horesT" id="horesT"><br><br>
        <label>Hores diaries</label><br>
        <input type="number" name="horesD" id="horesD"><br><br>

        <input type='submit' name='next' value='Afegir dades'/>
    </form>
</div>

1 个答案:

答案 0 :(得分:0)

在此行上:

var_dump($this->session->all_userdata());

您的会话为空是正常的,因为当您直接以“ else”状态通过时,您永远不会在其中插入数据。

如果将这样的行放在'if'条件下:

var_dump($this->session->all_userdata());
var_dump($this->session->userdata('curs'));

您看到userdate('curs')的东西了吗?