1

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

这是我的控制器控制器

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");

但我的 if else 只是做重定向,但注释的脚本标签工作正常。

如何使“味精”工作?

提前致谢。

4

2 回答 2

4

请在视图中添加 echo 语句,例如

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

或者

<?=$this->session->flashdata('msg')?>
于 2018-07-18T15:19:48.083 回答
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

于 2018-07-18T15:20:01.470 回答