Set BASE URL
In CodeIgniter Project, Open config.php file in config folder. Set value for base_url config as below:
$config['base_url'] = 'http://localhost:9092/LearnCodeIgniterWithRealApps/';
Create Account Model
Create new PHP file named account_model.php in models folder as below:
<?php
if (! defined('BASEPATH'))
exit('No direct script access allowed');
class Account_model extends CI_Model
{
function login($username, $password)
{
return $username == 'pmk' && $password == 'lab';
}
}
AutoLoad Account Model
In CodeIgniter Project, Open autoload.php file in config folder and set value for model config as below:
$autoload['model'] = array('Account_model' => 'accountModel');
AutoLoad Helpers
In CodeIgniter Project, Open autoload.php file in config folder. Add url and form helper to helper config as below:
$autoload['helper'] = array('url', 'form');
AutoLoad Libraries
In CodeIgniter Project, Open autoload.php file in config folder. Add session library to libraries config as below:
$autoload['libraries'] = array('session');
Create Controller
Create new PHP file named demo.php in controllers folder as below:
<?php defined('BASEPATH') or exit('No direct script access allowed');
class Demo extends CI_Controller
{
public function index()
{
$this->load->view('demo/index');
}
public function process_login()
{
$username = $_POST['username'];
$password = $_POST['password'];
if ($this->accountModel->login($username, $password)) {
$this->session->set_userdata('username', $username);
$this->load->view('demo/welcome');
} else {
$data['error'] = 'Invalid Account';
$this->load->view('demo/index', $data);
}
}
public function logout() {
$this->session->unset_userdata('username');
redirect('demo');
}
}
Set Default Controller
In CodeIgniter Project, Open routes.php file in config folder. Set value for default_controller as below:
$route['default_controller'] = 'demo';
Create View
Create new folder named demo in views folder. In this folder, create new views as below:
Index View
Create new PHP file named index.php as below:
<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
<head>
<title>Demo Page</title>
</head>
<body>
<h3>Login Form</h3>
<?php echo isset($error) ? $error : ''; ?>
<?php echo form_open('demo/process_login'); ?>
<table cellpadding="2" cellspacing="2">
<tr>
<td>Username</td>
<td>
<input type="text" name="username" required="required">
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" name="password" required="required">
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" value="Login">
</td>
</tr>
</table>
<?php echo form_close(); ?>
</body>
</html>
Welcome View
Create new PHP file named welcome.php as below:
<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
Welcome <?php echo $this->session->userdata('username'); ?>
<br>
<a href="<?php echo site_url('demo/logout'); ?>">Logout</a>
</body>
</html>
Structure of CodeIgniter Project
Run Application
Access index action in Demo controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/demo/index
Output
Test with invalid account is username: abc and password: 123
Output
Test with valid account is username: pmk and password: lab.
Output
Click logout link from success page to remove session and open login page again
Output
References
I recommend you refer to the books below to learn more about the knowledge in this article:
- CodeIgniter Web Application Blueprints
- CodeIgniter for Rapid PHP Application Development: Improve your PHP coding productivity with the free compact open-source MVC CodeIgniter framework!
- Programming with CodeIgniter MVC
- Murach’s PHP and MySQL (3rd Edition)
- Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning Php, Mysql, Javascript, Css & Html5)
- PHP and MySQL Web Development (5th Edition) (Developer’s Library)