Codeigniter:限制用户访问自己的个人资料页面

时间:2015-02-01 09:40:55

标签: php codeigniter profiles

我对Codeigniter相对较新,并试图限制配置文件所有者查看,编辑或删除用户配置文件。我已成功限制从另一个功能上的非登录用户访问配置文件。我的问题是将用户限制在他或她自己的个人资料中。

我已经让网址使用用户名,在我的数据库中名为' login。'我想要做的是将数据库中的用户名与网址相匹配。

任何提示和输入都会有所帮助。

控制器以确定用户是否已登录:

public function _is_logged_user() 
{ 
$id = $this->uri->segment(3); 
$data['user_profile'] = $this->user_model->get_user_profile($id);
$logged = $this->session->userdata('user_id');
   if($id == $logged) { 
     return TRUE; 
      }  else { 
     return FALSE; 
     }  
}

来自user_model的get_user_profile部分:

public function get($user_id = null) 
{ 

if ($user_id === null) { 
   $query = $this->db->get('user');
   } elseif(is_array($user_id)) {        
   $query = $this->db->get_where('user', $user_id);        
   } else { 
   $query = $this->db->get_where('user',['user_id' => $user_id]); 
   }         
   return $query->result_array();    
}  

1 个答案:

答案 0 :(得分:0)

在您的用户编辑控制器中,您应该检查登录的用户名(存储在$ _SESSION变量中)是否与正在编辑的用户匹配。

在codeigniter中,您可以使用$this->session->userdata()功能

来访问它

如果匹配,则显示编辑表单,否则显示一些错误消息。

因此,在您的用户编辑控制器中,您可能有。

if $this->_is_logged_user(){
    $data['user_profile'] = $this->user_model->get_user_profile($id);
    $this->display_edit($data);
} 
else{
    $this->display_error();
}

我可能无法在 _is_logged_user()函数中获取userdata

public function _is_logged_user() 
{ 
    $id = $this->uri->segment(3); 
    // no need for the line below, we're not using the data here
    // $data['user_profile'] = $this->user_model->get_user_profile($id);
    $logged = $this->session->userdata('user_id');
    ...
相关问题