Login Form with Session in CodeIgniter Framework


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

}

In CodeIgniter Project, Open autoload.php file in config folder and set value for model config as below:

$autoload['model'] = array('Account_model' => 'accountModel');




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');

In CodeIgniter Project, Open autoload.php file in config folder. Add session library to libraries config as below:

$autoload['libraries'] = array('session');

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');
    }

}

In CodeIgniter Project, Open routes.php file in config folder. Set value for default_controller as below:

$route['default_controller'] = 'demo';




Create new folder named demo in views folder. In this folder, create new views as below:

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>&nbsp;</td>
    				<td>
    					<input type="submit" value="Login">
    				</td>
    			</tr>
    		</table>
		<?php echo form_close(); ?>

	</body>
</html>

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>




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

I recommend you refer to the books below to learn more about the knowledge in this article: