如果登錄成功,如何使用用戶名設定會話?還有如何在用戶注銷時清除會話。謝謝
這是一個我做的登錄功能。登錄作業正常,資料庫也正常。我知道這不是好的代碼,但我剛剛開始在 Codeigniter 3 中使用 PHP。
enter code here
public function dologin(){
$this->load->helper('url');
$this->load->model('user_model');
$username = $this->input->post("username");
$pass = $this->input->post("password");
$result = $this->user_model->checkLogin($username, $pass);
if ($result == 1) {
redirect('messages/index');
} else {
redirect('user/login');
}
}
如果用戶和密碼存在,這是我的資料庫檢查登錄。
enter code here
public function checkLogin($username, $pass){
$hash =sha1($pass);
$this->db->from('Users');
$this->db->where('username', $username);
$this->db->where('password', $hash);
$query=$this->db->get();
if($query->num_rows() == 1){
return true;
}else{
return false;
}
}
uj5u.com熱心網友回復:
$result = $this->user_model->checkLogin($username, $pass);
// if data found in your database, then
if ($result == 1) {
$data = array(
'user_id' => $result[0]['id'],
'username' => $result[0]['username'],
'is_logged_in' => true,
);
$this->session->set_userdata($data);
//your session has been created
}
查看您的會話
print_r($this->session->userdata());
注銷或取消設定
$this->session->unset_userdata('user_id') == 'youruser_id');
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359569.html
上一篇:按ID洗掉JSON[]陣列元素
下一篇:CodeIgniter4專案中未定義的常量“CodeIgniter\Database\MySQLi\MYSQLI_STORE_RESULT”
