CodeIgniter登录和会话

编程入门 行业动态 更新时间:2024-10-26 12:22:17
本文介绍了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登录和会话

本文发布于:2023-07-13 03:13:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1099764.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CodeIgniter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!