在codeigniter中登录会话

时间:2015-10-20 07:21:49

标签: forms codeigniter session

如何通过确定用户登录会话来更新用户数据?现在我只是插入一个用户ID来测试我的代码是否有效但是我不知道如何使用会话。

我的控制器

com.example.Consumer1

我的模特

 public function index(){
    // check whether user login
    if($this->session->userdata('is_logged_in')){
            redirect('home');
        }
        else{
      $this->login();
        }
  }
function update_user() {


   $this->form_validation->set_rules('sirname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
   $this->form_validation->set_rules('name', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
   $this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email');

    if ($this->form_validation->run())
    {


            $data = array(
                'surname' => $this->input->post('sirname'),
                'name' => $this->input->post('name'),
                'email' => $this->input->post('email'),
            );

            $this->Account_model->update_user(31,$data); //change the 31 id to session??


        $this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully Updated!</div>');
        $this->load->library('form_validation');
      // redirect('home');
      }

      else 
      {
        $data['titile'] = 'Edit User';
        $data['display'] = 'displayname';
        $data['single_user'] = $this->Account_model->show_user(31);//change the 31 id to session??
        $this->load->view('template/header.php', $data);
        $this->load->view('update_view', $data);

      }
}

1 个答案:

答案 0 :(得分:0)

首先,您需要加载会话库。您还需要将用户的id(或uesername)设置为会话变量。确保在更新查询中使用该会话变量。

$userID = $this->session->userdata('id'); // Needes to be previously set after the successful login attempt
$this->Account_model->update_user($userID, $data);

顺便说一句,你需要在使用它之前加载form_validation库。

$this->load->library('form_validation');
$this->form_validation->set_rules(...);
相关问题