在 codeigniter 中更新会话
Update session in codeigniter
所以我必须在我的视图中进行配置,但是,在我注销之前它不会改变。
谁能帮我解决这个问题?
下面是我的登录控制器
public function login(){
$user = $this->input->post('username',true);
$pass = $this->input->post('password',true);
$cek= $this->M_Login->prosesLogin($user, $pass);
$hasil = count ($cek);
if($hasil >0){
$select =$this->db->get_where('usersystem',array('username'=>$user,'password'=>$pass))->row();
// $data = array('logged_in'=>true, 'loger'=>$select->username);*/
$this->session->set_userdata(array('username'=>$user,'idperson'=>$select->idperson,
'foto'=>$select->foto));
if($select->kdlevel=='klAd'){
redirect('C_Login/pageAdmin');
}elseif ($select->kdlevel=='klOr') {
redirect('C_Login/pageOrangtua');
}elseif ($select->kdlevel=='klG') {
redirect ('C_Guru/sikap');
}
}else{
$this->session->set_flashdata('err','username atau password salah');
redirect('C_Login/index');
}
}
这里是我上传头像控制器的方法
public function do_upload()
{
$this->load->library('upload');
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('uploadfoto', $error);
}
else
{
$person=$this->session->userdata('idperson');
$img=$this->upload->data();
$foto=$img['file_name'];
$data=array('foto'=>$foto);
$edit=$this->M_Upload->editData('usersystem', $data, $person);
// print_r($person);
if ($edit>0) {
$_SESSION['foto']=$edit;
redirect('upload/index');
}else{
echo "gagal";
}
}
}
这是我的个人资料图片视图
<div class="profile-userpic">
<img class="nav-user-photo" src="<?php echo base_url('uploads/'.$this->session->userdata('foto')); ?>" />
</div>
尝试更新照片会话变量。
您的代码
if ($edit>0) {
$_SESSION['foto']=$edit;
redirect('upload/index');
}else{
echo "gagal";
}
更新代码
// Returns array of containing all of the data related to the file you uploaded.
$upload_data = $this->upload->data();
$file_name = $upload_data['foto'];
// modify session
$this->session->set_userdata('foto', $file_name );
希望对您有所帮助:
您必须先取消设置 foto
会话,然后在您的 if 条件下再次设置它
你的else
条件应该是这样的:
$person = $this->session->userdata('idperson');
$img = $this->upload->data();
$foto = $img['file_name'];
$data = array('foto'=>$foto);
$edit = $this->M_Upload->editData('usersystem', $data, $person);
if ($edit > 0)
{
$this->session->unset_userdata('foto');
$this->session->set_userdata('foto', $foto);
redirect('upload/index');
}
else
{
echo "gagal";
}
更多:https://www.codeigniter.com/user_guide/libraries/sessions.html#removing-session-data
所以我必须在我的视图中进行配置,但是,在我注销之前它不会改变。 谁能帮我解决这个问题?
下面是我的登录控制器
public function login(){
$user = $this->input->post('username',true);
$pass = $this->input->post('password',true);
$cek= $this->M_Login->prosesLogin($user, $pass);
$hasil = count ($cek);
if($hasil >0){
$select =$this->db->get_where('usersystem',array('username'=>$user,'password'=>$pass))->row();
// $data = array('logged_in'=>true, 'loger'=>$select->username);*/
$this->session->set_userdata(array('username'=>$user,'idperson'=>$select->idperson,
'foto'=>$select->foto));
if($select->kdlevel=='klAd'){
redirect('C_Login/pageAdmin');
}elseif ($select->kdlevel=='klOr') {
redirect('C_Login/pageOrangtua');
}elseif ($select->kdlevel=='klG') {
redirect ('C_Guru/sikap');
}
}else{
$this->session->set_flashdata('err','username atau password salah');
redirect('C_Login/index');
}
}
这里是我上传头像控制器的方法
public function do_upload()
{
$this->load->library('upload');
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('uploadfoto', $error);
}
else
{
$person=$this->session->userdata('idperson');
$img=$this->upload->data();
$foto=$img['file_name'];
$data=array('foto'=>$foto);
$edit=$this->M_Upload->editData('usersystem', $data, $person);
// print_r($person);
if ($edit>0) {
$_SESSION['foto']=$edit;
redirect('upload/index');
}else{
echo "gagal";
}
}
}
这是我的个人资料图片视图
<div class="profile-userpic">
<img class="nav-user-photo" src="<?php echo base_url('uploads/'.$this->session->userdata('foto')); ?>" />
</div>
尝试更新照片会话变量。
您的代码
if ($edit>0) {
$_SESSION['foto']=$edit;
redirect('upload/index');
}else{
echo "gagal";
}
更新代码
// Returns array of containing all of the data related to the file you uploaded.
$upload_data = $this->upload->data();
$file_name = $upload_data['foto'];
// modify session
$this->session->set_userdata('foto', $file_name );
希望对您有所帮助:
您必须先取消设置 foto
会话,然后在您的 if 条件下再次设置它
你的else
条件应该是这样的:
$person = $this->session->userdata('idperson');
$img = $this->upload->data();
$foto = $img['file_name'];
$data = array('foto'=>$foto);
$edit = $this->M_Upload->editData('usersystem', $data, $person);
if ($edit > 0)
{
$this->session->unset_userdata('foto');
$this->session->set_userdata('foto', $foto);
redirect('upload/index');
}
else
{
echo "gagal";
}
更多:https://www.codeigniter.com/user_guide/libraries/sessions.html#removing-session-data