【问题标题】:Flash data not appearing in Codeigniter?Flash 数据没有出现在 Codeigniter 中?
【发布时间】:2017-08-23 11:28:06
【问题描述】:

我目前正在设计一个带有 codeigniter 的登录/注册系统。当我输入错误的密码或用户名时,除了 Flash 数据 之外,一切似乎都正常。

这就是我想要实现的,如果用户输入了不正确的凭据,则会显示闪存数据。

这是我的控制器的代码:

<?php
        //Extending the base controller
        class Auth extends CI_Controller{


       //calling the login view
       public function login(){
       //Only username and password credentials are needed here
        $this->form_validation->set_rules('username','Username','required');//using form_validation library
      $this->form_validation->set_rules('password','Password','required|min_length[5]');

      if($this->form_validation->run() == TRUE){

        $username = $_POST['username'];
        $password = md5($_POST['password']);//password hashing

        //Search for user in db
        $this->db->select('*');//query
        $this->db->from('users');
        $this->db->where(array('username' =>$username, 'password' => $password));
        $query = $this->db->get();//store in a var called query
        $user = $query->row();

        if($user->email){
          //temp message
          $this->session->set_flashdata("success","You are now logged in");

          //session variables
           $_SESSION['user_logged'] = TRUE;
           $_SESSION['username'] = $user->username;
           $_SESSION['email'] = $user->email;
           $_SESSION['password'] = $user->email;

           //redirect to their own profile page
           redirect("user/profile", "refresh");
        }else{
          $this->session->set_flashdata("error","No account exists, why not register with us?");
          redirect("auth/login", "refresh");

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

【问题讨论】:

  • 能否也提供视图(代码),因为您需要在那里输出 Flash 消息。
  • 请重新评估您的密码安全性。 MD5 就是剪不下来

标签: php codeigniter login


【解决方案1】:

在你的控制器中加载库session

$this->load->library('session');

在你的控制器中

$this->session->set_flashdata('error', 'No account exists, why not register with us?');
redirect("auth/login");

在你看来

<?php if($this->session->flashdata('error')){ ?>
    <h4 style="color:red;"><?php echo $this->session->flashdata('error'); ?></h4>

<?php } ?>

【讨论】:

    【解决方案2】:

    删除这个,“刷新”,因为它会清除它,或者你可以使用它来保留它一段时间。

    https://www.codeigniter.com/user_guide/libraries/sessions.html#tempdata

    // Both 'item' and 'item2' will expire after 300 seconds
    $this->session->mark_as_temp(array('item', 'item2'), 300);
    
    // 'item' will be erased after 300 seconds, while 'item2'
    // will do so after only 240 seconds
    $this->session->mark_as_temp(array(
            'item'  => 300,
            'item2' => 240
    ));
    

    也只是一个提示,您似乎没有对密码使用任何散列。我会使用http://php.net/manual/en/function.password-hash.php 并验证它http://php.net/manual/en/function.password-verify.php

    并设置会话https://www.codeigniter.com/user_guide/libraries/sessions.html#adding-session-data 确保您已在 config.php 等中设置会话保存路径

    【讨论】:

      猜你喜欢
      • 2018-03-28
      • 2016-09-10
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      相关资源
      最近更新 更多