[英]Why flashdata in CodeIgniter does not always work whereas userdata always works?
[英]Codeigniter 3 flashdata is always appearing
我正在使用 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');
}
從您的代碼中,每當身份驗證失敗時都會設置 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 始終顯示。
在視圖文件中使用上面的代碼...
<?php
if($this->session->flashdata('msg')
{
?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('msg');?>
</div>
<?php
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.