Learning Curve- Perfect Tutorial for the beginers
Follow us on Twitter Follow us on Facebook Follow us on Google Plus Follow us on rss
Skip to content
  • Home
  • APS1.0
  • APS2.0
  • PHP
  • SQ L& MYsql
  • Linux Shell Scripting
  • Codeigniter
  • Technical questions
  • Laravel
  • Angularjs
  • Node js
  • English Corner

Login Application using Codeigniter

By Admin | May 23, 2014
0 Comment

How to create a Login module in codeigniter.

After downloading all files from (codeigniter.zip) from CI website

You can create all files inside the application folder inside the CI.

Steps for Creating Login Application

1.Create a database in mysql using phpmydmin.After installling the xaamp/Waamp Type the below url into the browser.

http://localhost/phpmyadmin/

# create a database called testlogin.
# create table users inside the the db testlogin.for creating table users you can use the following queries

MySQL
1
2
3
4
5
6
CREATE TABLE IF NOT EXISTS `users` (
  `id` tinyint(4) NOT NULL AUTO_INCREMENT,
  `username` varchar(10) NOT NULL,
  `password` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
)

# insert the value with username=vasanthan and password=vasanthan

2.configure the routes.php in config folder.

sets the default_controller as like below.
$route[‘default_controller’] = “testlogindisplay”;

3.Change the values inside the database.php file inside the config folder

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$active_group = 'default';
$active_record = TRUE;
 
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'testlogin';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

4.create a model file in the model folder-user.php

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('id, 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;
   }
}
}
?>

4.we can create contorllers inside the controller folder. create the two files in the controller-testlogindisplay.php,homedisplay.php,verifylogindisplay.php

testlogindisplay.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Testlogindisplay extends CI_Controller {
 
function __construct()
{
   parent::__construct();
}
 
function index()
{
   $this->load->helper(array('form'));
   $this->load->view('testlogindisplay');
}
 
}
?>

homedisplay.php

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
<?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 Homedisplay 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('admindisplay', $data);
   }
   else
   {
     //If no session, redirect to login page
     redirect('testlogindisplay', 'refresh');
   }
}
 
function logout()
{
   $this->session->unset_userdata('logged_in');
   session_destroy();
   redirect('homedisplay', 'refresh');
}
 
}
 
?>

verifylogindisplay.php

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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Verifylogindisplay 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('testlogindisplay');
   }
   else
   {
     //Go to private area
     redirect('homedisplay', '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;
   }
}
}
?>

5.create view files inside the application/views folder-admindisplay.php,testlogindisplay.php

admindisplay.php

PHP
1
2
3
4
5
6
7
8
9
10
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>Login Demo-Admin Area</title>
</head>
<body>
   <h1>Home</h1>
   <h2>Welcome <?php echo $username; ?>!</h2>
   <a href="homedisplay/logout">Logout</a>
</body>
</html>

testlogindisplay.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>Login Demo</title>
</head>
<body>
   <h1>Simple Login with CodeIgniter</h1>
   <?php echo validation_errors(); ?>
   <?php echo form_open('verifylogindisplay'); ?>
     <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>

Category: Codeigniter Tags: importance of controller in codeigniter, login application using codeigniter, login demo using codeigniter, login script in codeigniter, login using username and password in codeigniter
Post navigation
← “ISnull” field in Mysql database table showing as Zero for a single record. Why? Fatal error: Call to undefined function form_open() in codeigniter with loading the following view file →

Recent Posts

  • How to change mysql engine from Myisam to Innodb
  • Error: MySQL shutdown unexpectedly in XAMPP
  • nodemon is not working in windows.nodemon -v is showing errors
  • Error [ERR_REQUIRE_ESM]: require() of ES Module E:\node-course\notes-app\node_modules\chalk\source\index.js from E:\node-course\app.js not supported. Instead change the require of index.js in E:\node-course\app.js to a dynamic import() which is available in all CommonJS modules. at Object. (E:\node-course\notes-app\app.js:8:15) { code: ‘ERR_REQUIRE_ESM’
  • Word Power

Recent Comments

  • Shrinivasa on Delete in Laravel with join condition
  • Vasanthan pv on Force Download in laravel-file download in laravel
  • Trey Mhundwa on Force Download in laravel-file download in laravel
  • Vasanthan pv on Link not working in codeigniter, base_url() displaying “”http://::1” why?
  • adil on Link not working in codeigniter, base_url() displaying “”http://::1” why?

Archives

Categories

Tweets about mydoubtsin
Follow @mydoubtsin
web
statistics
Copyright 2016
mydoubts.in
Iconic One Theme | Powered by Wordpress