Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Login extends CI_Controller {
- function __construct() {
- parent::__construct();
- $this->load->model('login_model','',TRUE);
- }
- public function index() {
- $this->load->helper('form');
- $this->load->library('form_validation');
- $this->form_validation->set_rules('username', 'Username', 'required');
- $this->form_validation->set_rules('password', 'Password', 'required');
- if ($this->form_validation->run() == FALSE) {
- if (isset($this->session->userdata['id'])) {
- redirect('home/profile');
- } else {
- //else it will redirect you to login page.
- $this->load->view('templates/header');
- $this->load->view('login');
- $this->load->view('templates/footer');
- }
- } else {
- //if it's a post request with valid data it will validate data in database.
- $username = $this->input->post('username');
- $password = md5($this->input->post('password'));
- $result = $this->login_model->login($username, $password);
- //This condition redirect you to homepage if you entered valid credentials
- if (count($result) !== 0) {
- $this->session->set_userdata('id', $username);
- redirect('home/profile');
- } else {
- //This will redirect you to login page with error.
- $this->session->set_flashdata('message', 'Login Fail!!<br>Invalid username or password!!');
- redirect('login');
- }
- }
- }
- }
- ?>
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Login_model extends CI_Model{
- function login($username, $password)
- $this->db->select('id','username','password');
- $this->db->from('users');
- $this->db->where('username', $username);
- $this->db->where('password', md5($password));
- $this->db->limit(1);
- $query = $this->db->get();
- if($query->num_rows()>1){
- return $query->result();
- }else{
- return false;
- }
- }
- }
- if( $query->num_rows() == 1 ){
- return $query->result();
- }
- $this->db->where('password', md5($password));
- $this->db->where('password', $password);
Add Comment
Please, Sign In to add comment