CodeIgniter 3闪存数据总是出现

tyg4sfes  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(95)

我使用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');
    }
tsm1rwdh

tsm1rwdh1#

在代码中,每当身份验证失败时,就会设置flashdata($this->empNumAuth()返回false)。因此,如果路径**/authentication**被调用,并且没有输入密码或用户名,它将空值传递给empNumAuth()函数,该函数肯定会返回false值(因为传递了空值,所以身份验证将失败)。否则,如果您觉得不应该显示flashdata,并且您确定提交了有效的凭据,那么问题就出在empNumAuth()函数中,请从那里开始检查。
额外的好处:如果你在error/success 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始终显示。

9wbgstp7

9wbgstp72#

在视图文件中使用上述代码。。

<?php
 if($this->session->flashdata('msg')
  {
?>
   <div class="alert alert-danger"><?php echo $this->session->flashdata('msg');?> 
   </div>
<?php 
  } 
?>
igetnqfo

igetnqfo3#

你使用PHP 8+吗?如果是这样,您的问题可能是由CodeIgniter版本引起的。我假设您使用的是旧版本的框架并使用PHP 8+。这个CodeIgniter更改日志页面包含有关PHP 8版本3.1.12之前的 Session flashdata bug的信息。因此,您可以通过将CodeIgniter版本升级到3.1.12或最新版本来解决您的问题。

相关问题