Simple Login with CodeIgniter in PHP

Simple Login with CodeIgniter in PHP

Simple Login with CodeIgniter
Simple Login with CodeIgniter 

CodeIgniter is an open source web application MVC structure Framework built in PHP designed to make your life as a programmer easier.  CodeIgniter allowing good speed and good performance when the site up and running. CodeIgniter details already previous post discussed CodeIgniter.

CodeIgniter Login  Database



CREATE TABLE `users` (
 `id` tinyint(4) NOT NULL AUTO_INCREMENT,
 `username` varchar(10) NOT NULL,
 `password` varchar(100) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;



insert into users (username, password) values ('dev2tricks', MD5('secret'));
                                                                 or
insert into users (username, password) values ('dev2tricks', base64('secret'));

Configure CodeIgniter

Database Access

Update the file application/config/database.php in your CodeIgniter installation with your database info:


$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'yourdbusername';
$db['default']['password'] = 'yourdbpassword';
$db['default']['database'] = 'yourdbname';


Default Controller

We need to tell CodeIgniter to land into our login page instead of the default welcome page.  Update the file application/config/routes.php in your CodeIgniter installation with you controller’s name.  We’ll call our landing controller login.

$route['default_controller'] = "login";

Libraries

In the file application/config/autoload.php you can configure the default libraries you want to load in all your controllers.

$autoload['libraries'] = array('database','session');
$autoload['helper'] = array('url');


 Model (application/models/user.php)

<?php

Class User 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;
   }
 }
}
?>



 Login Controller (application/controllers/login.php)



<?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'));
   $this->load->view('login');
 }

}

?>


Login View (application/views/login.php)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <title>Simple Login with CodeIgniter</title>
 </head>
 <body>
   <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>


Login Controller (application/controllers/login.php)



<?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;
   }
 }
}
?>

and Codeigniter database Table access insert,update delete click here


Simple Login with CodeIgniter in PHP Dev2Tricks 5 of 5
Simple Login with CodeIgniter in PHP Simple Login with CodeIgniter  CodeIgniter is an open source web application MVC structure Fra...

Share this

Related Posts

Previous
Next Post »

3 comments

comments
Anonymous
27 November 2017 at 03:22 delete

Una porquería tu copy paste.

Reply
avatar
25 May 2018 at 21:43 delete

nice post bro your article is Awsssm please help me checkout my website
www.gajabwap.blogspot.in

Reply
avatar
3 July 2024 at 21:38 delete

I thoroughly enjoyed this blog, thanks for sharing.

Reply
avatar