Codeigniter : Login, Check Login, Logout Function
controllers login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class User extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('User_model'); } public function login() { $this->load->view('user/login'); } public function validlogin() { if($this->input->server('REQUEST_METHOD') == TRUE){ if($this->User_model->record_count($this->input->post('username'), $this->input->post('password')) == 1) { $result = $this->User_model->fetch_user_login($this->input->post('username'), $this->input->post('password')); $this->session->set_userdata(array('login_id' => $result->id,'username' => $result->username,'display_name'=> $result->display_name)); redirect('order'); } else { $this->session->set_flashdata(array('msgerr'=> '<p class="login-box-msg" style="color:red;">ชื่อผู้ใช้หรือรหัสผ่านผิดพลาด!</p>')); redirect('user/login', 'refresh'); } } } public function logout() { $this->session->unset_userdata(array('login_id','username','display_name')); redirect('', 'refresh'); } public function profile() { $data['result'] = $this->User_model->read_user($this->session->userdata('login_id')); $this->load->view('user/profile',$data); } public function postprofile() { if($this->input->server('REQUEST_METHOD') == TRUE) { $this->form_validation->set_rules('display_name', 'ชื่อแสดง', 'required', array('required'=> 'ค่าห้ามว่าง!')); if($this->User_model->record_count($this->input->post('username'),$this->input->post('password')) == 1 && $this->form_validation->run() == TRUE){ $this->User_model->entry_user($this->session->userdata('login_id')); $this->session->set_userdata(array('display_name'=>$this->input->post('display_name'))); redirect('admin','refresh'); //ให้วิ่งไปหน้านีหน้าแรก สำหรับ admin }else{ redirect('user/profile','refresh'); } } } } |
controllers chklogin.php include all controllers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php class Loadpost { public function __construct() { $this->CI = & get_instance(); } public function check_login() { if ($this->CI->session->userdata('login_id') == NULL) { if ($this->CI->router->method != 'login' && $this->CI->router->method != 'validlogin' && $this->CI->router->method != 'listview' && $this->CI->router->method !='read') { redirect('user/login', 'refresh'); exit(); } }else{ if($this->CI->router->method=='login'){ redirect('','refresh'); exit(); } } } } |
models User_model.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php class User_model extends CI_Model { public function __construct() { parent::__construct(); } public function fetch_user_login($username,$password) { $this->db->where('username',$username); $this->db->where('password',$this->salt_pass($password)); $query = $this->db->get('tb_admin'); return $query->row(); } public function record_count($username,$password) { $this->db->where('username',$username); $this->db->where('password',$this->salt_pass($password)); return $this->db->count_all_results('tb_admin'); } public function salt_pass($password) { return md5($password); } public function read_user($id) { $this->db->where('id',$id); $query = $this->db->get('tb_admin'); if($query->num_rows() > 0){ $data = $query->row(); return $data; } return FALSE; } public function entry_user($id) { $data = array('admin_name' => $this->input->post('admin_name')); $this->db->update('tb_admin', $data, array('id'=> $id)); } } |
views login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<div class="login-box-body"> <p class="login-box-msg">เข้าใช้งานระบบ</p> <?=$this->session->flashdata('msgerr')?> <form method="post" action="<?php echo base_url('user/validlogin') ?>"> <div class="form-group has-feedback"> <input type="text" placeholder="Username" class="form-control" name="username"> <span class="glyphicon glyphicon-user form-control-feedback"></span> </div> <div class="form-group has-feedback"> <input type="password" placeholder="Password" class="form-control" name="password"> <span class="glyphicon glyphicon-lock form-control-feedback"></span> </div> <div class="row"> <div class="col-xs-4"> <button class="btn btn-primary btn-block btn-flat" type="submit">Sign In</button> </div><!-- /.col --> </div> </form> </div><!-- /.login-box-body --> |