CodeIgniter登录和会话 [英] CodeIgniter login and session

查看:83
本文介绍了CodeIgniter登录和会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为学生注册建立网络,并且正在使用CodeIgniter.我已经设法将表单插入到数据库中,并且正在使用提交表单中的电子邮件和密码进行登录.我的登录页面出现问题,以某种方式它无法检查数据库中是否存在电子邮件或密码,并且也无法重定向到我想要的页面.

I am currently building a web for student registration and I am using CodeIgniter. I already managed to insert the form to my database and I am using email and password from the submitted form to login. I got a problem in my login page,somehow it cant check either the email or password exists on the database or not and it cant redirect to my desired page either.

这是我的控制器

public function index()
{
    $data = '';
    if($this->input->post('submit'))
    {
        $this->form_validation->set_rules('email','email','required');
        $this->form_validation->set_rules('password','password','md5|required');

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

            $this->load->model('testmodel','user');

            $member = $this->user->selectUser("WHERE email = '$email' AND password = '$password'");

            if($member->has_rows())
            {
                $user_data = array(
                                'name'  => $member->row('name'),
                                'nisn'  => $member->row('nisn'),
                                'email'     => $member->row('email'),
                                'logged_in_admin' => TRUE
                           );

                $this->session->set_userdata($user_data); redirect('pages/userpage/userpage');
            }else 
            {
                $data['message'] = '<div class = "error">Username and password wrong!</div>';
            }
        }else 
        {
            $data['message'] = '<div class = "error">' . validation_errors() .  '</div>';
        }
    }

    $this->load->view('pages/userlogin/userlogin',$data);
}

这是testmodel中的selectuser函数

This is the selectuser function in testmodel

public function selectUser($where = '', $sort = 'DESC', $limit = '')
{
    $query = "
        SELECT SQL_CALC_FOUND_ROWS * FROM student_table
            $where
        ORDER BY ticketnumber $sort
            $limit
    ";
    return $this->db->query($query);
}'

推荐答案

尝试使用此代码登录以使用会话.

Try this code to login with the use of session.

public function index()
{
  //get the posted values
  $email= $this->input->post("email");
  $password = $this->input->post("password");

  //set validations
  $this->form_validation->set_rules("email", "Email", "trim|required");
  $this->form_validation->set_rules("password", "Password", "trim|required");

  if ($this->form_validation->run() == FALSE)
  {
       //validation fails
       $this->load->view('Your_Login_View');
  }
  else
  {
       //validation succeeds
       if ($this->input->post('btn_login') == "Login")
       {
            //check if Email and password is correct
            $usr_result = $this->testmodel->selectUser($email, $password);
            if ($usr_result > 0) //active user record is present
            {
                 //set the session variables
                 $_SESSION['email'] = $email;
                 $_SESSION['loginuser'] = true;

                redirect("Your_Desired_page");
            }
            else
            {
                 $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Invalid email and password!</div>');
                 redirect('admin');
            }
       }
       else
       {
            redirect('admin');
       }
    }
}

这篇关于CodeIgniter登录和会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆