drcmg28484
drcmg28484
2018-06-09 06:39
浏览 27
已采纳

在codeigniter中注销时删除令牌

I am assigning the user_id to my session as soon as the user logs in, and I am giving a token to the user when he logs in, and I am storing it in the database just to keep the record. Now, I want to delete that token when the user clicks logout and for that,

view

<a href=" <?php base_url('user/logout') ?> ">logout</a>

controller

public function logout(){

$uid = $this->session->userdata('user_id');
$this->load->model('loginmodel');
$lid = $this->loginmodel->deltoken($uid);

if($lid){

  $this->session->unset_userdata('user_id');

  return redirect(user);

}else {
  echo "fail";
}
}

model

public function deltoken($uid)
{
    $query = $this->db->query("DELETE FROM user_auth WHERE id = '$uid'");
    return TRUE;
}

but in the database record belonging to that particular id is not getting deleted. what am i doing wrong here? Thank you for your suggestions

图片转代码服务由CSDN问答提供 功能建议

我在用户登录后立即将 user_id 分配给我的会话,并且 我在登录时向用户提供令牌,我将其存储在数据库中以保存记录。 现在,我想在用户点击退出时删除该令牌,为此,

查看

   &lt; a href =“&lt;?php base_url('user / logout')?&gt;”&gt; logout&lt; / a&gt; 
   
 
 

控制器

  public function logout(){
 
 $ uid = $ this-&gt; session-&gt; userdata('user_id'); 
 $  this-&gt; load-&gt; model('loginmodel'); 
 $ lid = $ this-&gt; loginmodel-&gt; deltoken($ uid); 
 
if($ lid){
 
 $ this this  - &gt; session-&gt; unset_userdata('user_id'); 
 
返回redirect(用户); 
 
} else {
 echo“fail”; 
} 
} 
    
 
 

模型

 公共函数deltoken($ uid)
 {
 $ query = $ this  - &gt; db-&gt; query(“DELETE FROM user_auth WHERE id ='$ uid'”); 
返回TRUE; 
} 
   
 
 

但是 属于该特定id的数据库记录不会被删除。 我在这里做错了什么? 谢谢你的建议

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujiang2641
    doujiang2641 2018-06-09 07:07
    已采纳

    Hope this will help you :

    You are missing echo in your link, should be like this :

    <a href=" <?php echo site_url('user/logout') ?> ">logout</a>
    

    Your model deltoken method should be like this :

    public function deltoken($uid)
    {
        $uid  = ! empty($uid) ? $uid : $this->session->user_data('user_id');
        $this->db->where('id' ,$uid);
        $this->db->delete('user_auth');
        if ( $this->db->affected_rows() )
        {
          return TRUE;
        }
        else
        {
          return FALSE;
        } 
    }
    
    点赞 评论

相关推荐