CodeIgniter is an open supply net software framework in-built PHP designed to make your life as a programmer easier, whereas permitting you good pace for development, and likewise good performance when the website online is up and operating.
Being a Java developer for nearly 10 years now, after I had to transfer to PHP I selected CodeIgniter for the following causes:
- easy to install and configure (being a novice in PHP this used to be the most important)
- smooth and elegant MVC implementation
- uses active report pattern for database get admission to
- overall small footprint and excellent efficiency
on a regular basis if you end up building a application, the login/logout functionality is a should we always have to move thru, so this fast tutorial will focal point on this functionality, taking advantage of the benefits of the usage of CodeIgniter instead of doing it from scratch in PHP.
necessities
- CodeIgniter framework. by the point this tutorial was once finished, the latest model was once 2.0.2
- Any Apache/PHP/MySQL stack. that you can install the purposes independently, or set up a type of packages which have all of them bundled collectively.
installing CodeIgniter
to install CodeIgniter, you handiest need to uncompress the Zip file you obtain from the site into your htdocs directory and you’re good to move. We’ll configure the database get right of entry to later.
Create the database
For this tutorial, you desire a MySQL database with the following table:
CREATE table `customers` (
`identity` tinyint(4) not NULL AUTO_INCREMENT,
`username` varchar(10) not NULL,
`password` varchar(a hundred) not NULL,
main KEY (`identity`)
) ENGINE=MyISAM DEFAULT CHARSET=lat
remember additionally to add at least one consumer. We’ll add one person referred to as bob with passwordsupersecret.
insert into customers (username, password) values ('bob', MD5('supersecret'));
Configure CodeIgniter
Database get right of entry to
replace 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 wish to tell CodeIgniter to land into our login page as an alternative of the default welcome page. replace the file utility/config/routes.php on your CodeIgniter set up with you controller’s title. We’ll call our landing controller login.
$route['default_controller'] = "login";
Default Libraries
within the file software/config/autoload.php which you could configure the default libraries you need to load in all of your controllers. For our case, we’ll load the database and session libraries, on account that we wish to deal with user periods, and likewise the URL helper for inside link generation
$autoload['libraries'] = array('database','session');
$autoload['helper'] = array('url');
Encryption Key
whilst you use the session library, you wish to set the encryption_key within the fileapplication/config/config.php.
$config['encryption_key'] = 'REALLY_LONG_NUMBER';
The Code
listed below are the actual Views, Controllers and variation we are the usage of for the login performance.
consumer variation (utility/models/user.php)
<?php
type user extends CI_Model
perform login($username, $password)
$this -> db -> make a selection('id, username, password');
$this -> db -> from('users');
$this -> db -> where('username', $username);
$this -> db -> the place('password', MD5($password));
$this -> db -> restrict(1);
$question = $this -> db -> get();
if($query -> num_rows() == 1)
return $question->end result();
else
return false;
?>
Login Controller (application/controllers/login.php)
<?php if ( ! outlined('BASEPATH')) exit('No direct script get right of entry to allowed');
type Login extends CI_Controller
perform __construct()
father or mother::__construct();
operate index()
$this->load->helper(array('type'));
$this->load->view('login_view');
?>
Login View (utility/views/login_view.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>easy Login with CodeIgniter</title>
</head>
<physique>
<h1>simple Login with CodeIgniter</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('verifylogin'); ?>
<label for="username">Username:</label>
<input kind="textual content" size="20" identity="username" name="username"/>
<br/>
<label for="password">Password:</label>
<input kind="password" measurement="20" id="passowrd" title="password"/>
<br/>
<enter type="put up" value="Login"/>
</type>
</physique>
</html>
VerifyLogin Controller (utility/controllers/verifylogin.php)
This controller does the actual validation of the fields and checks the credentials towards the database.
<?php if ( ! outlined('BASEPATH')) exit('No direct script get admission to allowed');
classification VerifyLogin extends CI_Controller
operate __construct()
mother or father::__construct();
$this->load->model('person','',authentic);
function index()
//This way can havecallback_check_database');
if($this->form_validation->run() == FALSE)
//container validation failed. user redirected to login page
$this->load->view('login_view');
else
//Go to private space
redirect('dwelling', 'refresh');
perform check_database($password)
//container validation succeeded. Validate in opposition to database
$username = $this->input->post('username');
//query the database
$end result = $this->person->login($username, $password);
if($end result)
$sess_array = array();
foreach($result as $row)
$sess_array = array(
'identity' => $row->identification,
'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;
?>
house Controller (software/controllers/house.php)
this is the non-public page (most effective authenticated customers can get entry to it).
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we want to name PHP's session object to access it thru CI
classification residence extends CI_Controller
operate __construct()
father or mother::__construct();
perform index()
if($this->session->userdata('logged_in'))
$session_data = $this->session->userdata('logged_in');
$knowledge['username'] = $session_data['username'];
$this->load->view('home_view', $knowledge);
else
//If no session, redirect to login web page
redirect('login', 'refresh');
function logout()
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('dwelling', 'refresh');
?>
house page View (application/views/home_view.php)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.zero 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 - non-public house</title>
</head>
<physique>
<h1>home</h1>
<h2>Welcome <?php echo $username; ?>!</h2>
<a href="home/logout">Logout</a>
</body>
</html>
The code is lovely easy to observe and consider. also, you could download the code from here, so which you could set up it and test it on your location. You’ll handiest desire a full installation of CodeIgniter 2.0.2 and the table for your MySQL database. if you would like any assist, be happy to leave us a remark or shoot us an e mail.
also, this code makes use of an attractive common type validation from CodeIgniter. if you need a more advanced validation process, take a look at CodeIgniter’s kind Validation docs at their website online.
Cheers!
Easy Login System with CodeIgniter
No comments:
Post a Comment