我正在使用 codeigniter 3 和 flashdata 在插入無效的用戶名或密碼時向用戶顯示錯誤訊息。
問題是,即使頁面剛剛打開并且沒有插入密碼或用戶名,訊息也會始終顯示。
編輯:比較和身份驗證是正確的,但是當我打開頁面時,即使沒有采取任何行動,它仍然會出現。
這是表格
<?php echo $this->session->flashdata('msg');?>
這是控制器
public function authentication(){
//post user unput
$empNum=$this->input->post('employeeNum');
$pwd=$this->input->post('password');
$user=$this->empNumAuth($empNum, $pwd);
if($user) {
if($user['PrivilegeLevel']==='1'){
$this->session->set_userdata($user);
redirect('AdminDashboard/view');
}
else if($user['PrivilegeLevel']=='2') {
$this->session->set_userdata($user);
redirect('UserDashboard/view');
}
}
else {
$this->session->set_flashdata('msg','????? ??????? ?? ??? ?????? ????');
redirect('Login/LoginPage');
}
uj5u.com熱心網友回復:
從您的代碼中,每當身份驗證失敗時都會設定 flashdata($this->empNumAuth() 回傳 false)。因此,如果路徑/authentication被呼叫,并且沒有輸入密碼或用戶名,它會將空白值傳遞給 empNumAuth() 函式,該函式肯定會回傳一個錯誤值(因為傳遞了空白值,因此身份驗證將失敗)。否則,如果您認為不應顯示 flashdata,并且您確定已提交有效憑據,則問題出在 empNumAuth() 函式內,請從那里開始檢查。
獎勵:如果您在錯誤/成功 div 中回顯 flashdata 訊息,它將始終顯示,上面沒有文本。也就是說,假設您有一個紅色背景格式的錯誤 div,當您直接回顯 flashdata 時,錯誤 div 將顯示但帶有空白文本。最好的方法是在回顯之前檢查 flashdata 是否已設定。請參閱下面的示例。(我使用了默認的 Bootstrap 錯誤警報 div)
<?php if($this->session->flashdata('msg'){?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('msg');?>
</div>
<?php } ?>
上面的代碼檢查是否設定了 flashdata,這將阻止 flashdata div 始終顯示。
uj5u.com熱心網友回復:
在視圖檔案中使用上面的代碼...
<?php
if($this->session->flashdata('msg')
{
?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('msg');?>
</div>
<?php
}
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/366893.html
