如何创建新的会话来替换旧的会话CodeIgniter

时间:2016-10-07 00:43:08

标签: codeigniter session

我在用户登录时设置会话 用户模型

function cek(){
$username=$this->input->post('username');
$password=md5($this->input->post('password'));
$query=$this->db->query("select akun.*,profile.kelas, profile.jurusan FROM profile right JOIN akun ON profile.id_akun = akun.id_akun WHERE akun.username = '$username' AND akun.password = '$password'");
   if ($query->num_rows() > 0) 
    foreach ($query->result() as $data) {
      $data=array('LOGIN'=>TRUE,'LEVEL'=>$data->level,'KELAS'=>$data->kelas,'JURUSAN'=>$data->jurusan,'ID_AKUN'=>$data->id_akun,'USERNAME'=>$data->username);
      $this->session->set_userdata($data);  
      echo json_encode(array("status" => TRUE));  

  } else {
   echo json_encode(array("status" => FALSE));
  }     

} 我想替换会话KELAS和JURUSAN

个人资料控制器

public function tambahprofile()
        {
            if($this->session->userdata('LOGIN')=='TRUE' and $this->session->userdata('LEVEL')==1 )
            {
                $id_akun=$this->session->userdata('ID_AKUN');
                $kelas='12';
               $jurusan='multimedia';
                $this->validasi();
                $data = array(
                    'nis' =>$this->input->post('nis'), 
                    'id_akun' =>$id_akun, 
                    'nama' =>$this->input->post('nama'),
                    'telpon' =>$this->input->post('telpon'),
                    'alamat' =>$this->input->post('alamat'),
                    'tgllahir' =>$this->input->post('tgllahir'),
                    'tmptlahir' =>$this->input->post('tmptlahir'),
                    'jurusan' =>$this->input->post('jurusan'),
                    'kelas' =>$this->input->post('kelas'),
                    'jk' =>$this->input->post('jk'),

                    );
                if ($this->input->post('id')=='') {
                    $this->profile_model->tambah($data);
                    /**

                        TODO:
                        - First todo item
                        - Second todo item

                     */


                 echo json_encode(array("status" => TRUE));
                }else{
                     $this->profile_model->update(array('id' => $this->input->post('id')),$data);
                       $this->profile_model->update($data);

                 echo json_encode(array("status" => TRUE));
                }
              }else {
            redirect('beranda/loginPage');     
            }

            }

查看

<?php echo $this->session->userdata('KELAS').$this->session->userdata('JURUSAN');?>

我正在创建新会话以替换旧会话KELAS和jurusan 没有错误,但在我看来是旧会话

2 个答案:

答案 0 :(得分:0)

当您需要更换旧数据时,只需取消设置之前的数据,然后重新设置新数据

//remove old data
$perticular_data = array('KELAS'=>$kelas,'JURUSAN'=>$jurusan);
$this->session->unset_userdata($perticular_data);

//set new data
$perticular_data = array('KELAS'=>$new_kelas,'JURUSAN'=>$new_jurusan);
$this->session->set_userdata($perticular_data);

答案 1 :(得分:0)

试试这个。

$data = array(
                'kelas' => "test1",
                'jurusan' => "Test2"
            );
$this->session->set_userdata($data);

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

$this->session->unset_userdata('kelas');
$data = array( 'kelas' => "newTest" );
$this->session->set_userdata($data);

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

使用$this->session->unset_userdata( 'kelas' );将仅取消设置该条目。 $data = array( 'kelas' => "newTest" ); $this->session->set_userdata($data);将仅替换该条目。

在旁注$password=md5( $this->input->post( 'password' ) );,不建议交配。如果您读取md5()可以被破坏的速度,则使其成为存储密码的错误选项。

PHP具有内置散列函数password_hash()

http://php.net/manual/en/function.password-hash.php

值得一读。