【问题标题】:codeigniter, set_flashdata not workingcodeigniter,set_flashdata 不工作
【发布时间】:2018-07-18 15:12:13
【问题描述】:

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

这是我的控制器控制器

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 只是进行重定向,但注释的脚本标签工作正常。

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

提前致谢。

【问题讨论】:

    标签: php codeigniter codeigniter-3 codeigniter-flashdata


    【解决方案1】:

    请在视图中添加回显语句

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

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

    【讨论】:

    • session->flashdata ('味精'); ?>
      这为我解决了
    • 有时我也会这样:)
    • 如果答案解决了您的问题,请投票支持社区的共同努力。谢谢
    • 我需要 15 个声望才能投票。你能投票支持我先达到 15 吗?
    【解决方案2】:

    应该是这样的:

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

    <?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

    【讨论】:

    • 不能在 D:\xamp\htdocs\login_pagination\application\views\login_v 中对函数调用的结果使用 isset()(您可以改用“null !== func()”)第 24 行的 .php 我现在收到此错误
    • isset 更改为 empty ,刚刚更新了我的答案,请检查一下
    猜你喜欢
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    相关资源
    最近更新 更多