codeigniter,set_flashdata不起作用

时间:2018-07-18 15:12:13

标签: php codeigniter codeigniter-3 codeigniter-flashdata

我在堆栈溢出中读取了仅在下一个服务器请求之前有效的闪存数据,因此我制作了新的闪存数据以显示几个消息。 下面是我的代码

这是我的控制器Controller

public function login(){
        $this->form_validation->set_rules('username','Username','required');
        $this->form_validation->set_rules('password','Password','required|min_length[5]');


        if($this->form_validation->run() == TRUE){
            $username= $this->input->post('username');
            $password= $this->input->post('password');

            $this->load->model('Auth_model');
            $user = $this->Auth_model->get_login();

           if ($user == 0) {
                    //echo "<script>alert('wrong username');</script>";

                    $this->session->set_flashdata("msg","Username does not exists");
                    redirect("auth/login");
           }
           else{
            print_r($user['username']); 
            if($username == $user['username'] && $password == $user['password']){
                $this->session->set_flashdata("success","You are logged in");
                $_SESSION['user_logged'] = TRUE;
                $_SESSION['username'] = $user['username'];
                redirect("user/dashboard","refresh");
            }
            else {

                //echo "<script>alert('wrong password');</script>";
                $this->session->set_flashdata("msg","Password does not match.");
                redirect("auth/login");
            }

        }
        }
        $this->load->view('login_v');
    }

模型

public function get_login(){

        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));

            $this->db->select('*');
            $this->db->from('users');
            $this->db->where(array('username' => $username));
            $query = $this->db->get();

            $user = $query->row();

            if ($this->db->affected_rows() != 1) {
                return false;
            } 
            else {
            $data = array(
                'user_id' => $user->user_id,
                'username' => $user->username,
                'password' => $user->password
            );
            //print_r($data);
            //$this->session->set_userdata($data);
            return $data;
        }
    }

视图

  <?php if(isset($_SESSION['success'])) {?>
    <div class="alert alert-success"><?php echo $_SESSION['success']; ?></div>
  <?php } ?>
  <?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
  <?php $this->session->flashdata('msg');?>
  <form action="" method="POST">
    <div class="form-group">
      <label for="username">Username</label>
      <input type="text" class="form-control" name="username" id="username">
    </div>
    <div class="form-group">
      <label for="password">Password:</label>
      <input type="password" class="form-control" name="password" id="password">
    </div>
    <div>
      <button class="btn btn-primary" name="login">Login</button>
    </div>
  </form>

我要显示

$this->session->set_flashdata("msg","Username does not exists");

但是如果我只是在做重定向,虽然注释的脚本标记仍然可以正常工作。

如何使“味精”发挥作用?

谢谢。

2 个答案:

答案 0 :(得分:3)

请在类似的视图中添加echo语句

 <?php echo $this->session->flashdata('msg');?>

OR

<?=$this->session->flashdata('msg')?>

答案 1 :(得分:1)

应该是这样的:

使用flashdata来获取key,应该是这样

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

或者只是这样做:

<div class="alert alert-success"><?php echo $this->session->flashdata('msg'); ?></div>

更多信息:https://www.codeigniter.com/user_guide/libraries/sessions.html