
1. Database table
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
INSERT INTO `users` (`id`, `username`, `password`) VALUES
(1, 'admin', 'admin');
2. Set application\config\routes.php
$route['default_controller'] = "login";
$route['404_override'] = '';
3. set autoload application\config\autoload.php
$autoload['libraries'] = array('database', 'session');
4. set the encryption_key
application\config\config.php
$config['encryption_key'] = 'REALLY_LONG_NUMBER';
Create Model application\models\user.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 | <?php Class User extends CI_Model { function login($username, $password) { $ this -> db -> select( 'username, password' ); $ this -> db -> from( 'users' ); $ this -> db -> where( 'username = ' . "'" . $username . "'" ); $ this -> db -> where( 'password = ' . "'" . $password . "'" ); $ this -> db -> limit(1); $query = $ this -> db -> get(); if ($query -> num_rows() == 1) { return $query->result(); } else { return false ; } } } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php if ( ! defined( 'BASEPATH' )) exit ( 'No direct script access allowed' ); class Login extends CI_Controller { function __construct() { parent::__construct(); } function index() { $ this ->load->helper(array( 'form' , 'url' )); $ this ->load->view( 'login_view' ); } } ?> |
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 | <?php if ( ! defined( 'BASEPATH' )) exit ( 'No direct script access allowed' ); session_start(); //we need to call PHP's session object to access it through CI class Home extends CI_Controller { function __construct() { parent::__construct(); } function index() { if ($ this ->session->userdata( 'logged_in' )) { $session_data = $ this ->session->userdata( 'logged_in' ); $data[ 'username' ] = $session_data[ 'username' ]; $ this ->load->view( 'home_view' , $data); } else { //If no session, redirect to login page redirect( 'login' , 'refresh' ); } } function logout() { $ this ->session->unset_userdata( 'logged_in' ); session_destroy(); redirect( 'home' , 'refresh' ); } } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <head> <title>Simple Login with CodeIgniter</title> </head> <body bgcolor= "pink" > <h1>Simple Login with CodeIgniter</h1> <?php echo validation_errors(); ?> <?php echo form_open( 'verifylogin' ); ?> <label for = "username" >Username:</label> <input type= "text" size= "20" id= "username" name= "username" /> <br/> <label for = "password" >Password:</label> <input type= "password" size= "20" id= "passowrd" name= "password" /> <br/> <input type= "submit" value= "Login" /> </form> </body> </html> |
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 | <?php if ( ! defined( 'BASEPATH' )) exit ( 'No direct script access allowed' ); class VerifyLogin extends CI_Controller { function __construct() { parent::__construct(); $ this ->load->model( 'user' , '' ,TRUE); } function index() { //This method will have the credentials validation $ this ->load->library( 'form_validation' ); $ this ->form_validation->set_rules( 'username' , 'Username' , 'trim|required|xss_clean' ); $ this ->form_validation->set_rules( 'password' , 'Password' , 'trim|required|xss_clean|callback_check_database' ); if ($ this ->form_validation->run() == FALSE) { //Field validation failed. User redirected to login page $ this ->load->view( 'login_view' ); } else { //Go to private area redirect( 'home' , 'refresh' ); } } function check_database($password) { //Field validation succeeded. Validate against database $username = $ this ->input->post( 'username' ); //query the database $result = $ this ->user->login($username, $password); if ($result) { $sess_array = array(); foreach($result as $row) { $sess_array = array( //'id' => $row->id, 'username' => $row->username ); $ this ->session->set_userdata( 'logged_in' , $sess_array); } return TRUE; } else { $ this ->form_validation->set_message( 'check_database' , 'Invalid username or password' ); return false ; } } } ?> |
1 2 3 4 5 6 7 8 9 10 | <head> <title>Simple Login with CodeIgniter - Private Area</title> </head> <body bgcolor= "pink" > <h1>Home</h1> <h2>Welcome <?php echo $username; ?>!</h2> <a href= "home/logout" >Logout</a> </body> </html> |