Codeigniter会话未显示数据

时间:2018-09-23 22:02:39

标签: php codeigniter

我的代码中出现以下错误: 1)我在会话中存储的数据未显示在视图中 2)表单验证集规则不起作用。每当我按下登录而不输入任何字段时,它都会打印一个。

我有自动加载的会话和表单验证。

我应该使用php会话还是ci会话?

控制器:

public function loginFormValidation()
      {
          $this->form_validation->set_rules('email','Username','required'); //not working
          $this->form_validation->set_rules('pass','Password','required');
          $this->form_validation->set_error_delimiters('<div class = "text-danger">', '</div>');

          if($this->form_validation->run())
          {
              $email = $this->input->post('email');
              $pass = $this->input->post('pass');

              $this->load->model('loginModel');
              $result = $this->loginModel->loginValidation($email,$pass);

              $user_id = $result->user_id;
              $user_name = $result->user_name;
              $password = $result->password;



              $arrayDb = array(
                  'user_id' => $user_id,
                  'user_name' => $user_name,
                  'password' => $password,
              );

              $this->session->set_userdata('row', $arrayDb);

              header("location:".base_url()."/Users/dashboard");

          }
          else
          {
              echo "a";
          }
      }

型号:

class loginModel extends CI_Model
{
    public function loginValidation($email,$pass)
    {
        $q = $this->db->where(['email' => $email, 'password' => $pass])
            ->get('users');

        if($q->num_rows())
        {
            return $q->row();
        }
        else
        {
            return false;
        }
    }
}

我要在其中打印以进行测试的控制器:

class Users extends CI_Controller
{
    public function dashboard()
    {
        echo $this->session->userdata('user_name');  // Empty no data showing
        exit;
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码有一些错误和疏漏。

  1. 永远不要存储纯文本密码,更不要说将其添加到会话变量中了。没有必要。检出:http://php.net/manual/en/function.password-verify.php
  2. 您需要检查登录功能是否实际返回了数据,否则只有任何人只要通过表单验证即可“登录”。
  3. 您要根据访问变量的方式错误地使用数组设置会话数据。

控制器:

public function loginFormValidation() {
    $this->form_validation->set_rules('email', 'Username', 'required');
    $this->form_validation->set_rules('pass', 'Password', 'required');
    $this->form_validation->set_error_delimiters('<div class = "text-danger">', '</div>');

    if ($this->form_validation->run()) {
        $email = $this->input->post('email');
        $pass = $this->input->post('pass'); // do not store plaintext!!!

        $this->load->model('loginModel');
        $result = $this->loginModel->loginValidation($email, $pass);

        if ($result) {
            $user_id = $result->user_id;
            $user_name = $result->user_name;
            $password = $result->password;
            //https://www.codeigniter.com/user_guide/libraries/sessions.html#adding-session-data
            $arrayDb = array(
                'user_id' => $user_id,
                'user_name' => $user_name,
                'password' => $password, // why are you storing their PLAINTEXT password in a session?!
            );
            $this->session->set_userdata($arrayDb);
            header("location:" . base_url() . "/Users/dashboard");
        } else {
            echo 'login failed; bad username/password.';
        }
    } else {
        echo validation_errors();
    }
}

型号:

public function loginValidation($email, $pass) {
    $q = $this->db->where(['email' => $email, 'password' => $pass])
            ->get('users');
    if ($q->num_rows() == 1) {
        return $q->row();
    }
    return false;
}

而不是重新发明轮子,请结帐:https://github.com/benedmunds/CodeIgniter-Ion-Auth 我个人推荐它。