Well... I'm out of ideas on what to write about. Do you have any ideas? Please, write me in any comment section you find, and I will do my best to tell you about that particular subject

Let’s install Ion Auth library

Created at: October 28, 2015; Last update: June 30, 2016

What will we learn in this episode

In this episode we will learn about setting the CodeIgniter framework to work with sessions and importing the necessary files of the Ion Auth library and create the login form.

Setting up CodeIgniter to work with sessions

Just to make sure everything is OK, we will start by having a fresh install of CodeIgniter. We will install it directly inside the htdocs or www (assuming you have XAMMP installed or some other kind of wamp or lamp stack installed). This way we can access the site by typing : “http://localhost/” in our browser. If you already installed sessions correctly, you can skip this step.

We will use the database driver for keeping the session data. In order to do this we will go to our config file (application/config/config.php) and look for the following lines (more or less…):

PHP

1

2

3

4

5

6

7

$config['sess_driver']='files';

$config['sess_cookie_name']='ci_session';

$config['sess_expiration']=7200;

$config['sess_save_path']=NULL;

$config['sess_match_ip']=FALSE;

$config['sess_time_to_update']=300;

$config['sess_regenerate_destroy']=FALSE;

In here we need to change the “sess_driver” value and the “sess_save_path”. The “sess_driver” value should be set to database, while the “sess_save_path” should have the name of the table used for sessions. If we read the great manual of CodeIgniter we will see what values we should put in there:

PHP

1

2

3

4

5

6

7

$config['sess_driver']='database';

$config['sess_cookie_name']='ci_session';

$config['sess_expiration']=7200;

$config['sess_save_path']='ci_sessions';

$config['sess_match_ip']=FALSE;

$config['sess_time_to_update']=300;

$config['sess_regenerate_destroy']=FALSE;

Now, let’s go to our autoload.php (application/config/autoload.php) and autoload the session library. Look for the line:

PHP

1

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

…and add session as an element of the array:

PHP

1

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

While we’re here, let’s also activate the url helper:

PHP

1

$autoload['helper']=array('url');

If now we visit our website from the browser we will see that an error has appeared:

Why? Because we’ve promised the framework a table to keep our session data, but we didn’t create that specific table. So let’s create that table. Do you have a database set up for your website? If not, follow this tutorial on how to set up a database.

Returning to our sessions, in order to use them we need to create a table named ci_sessions. The table structure is also presented in the fine manual of CodeIgniter:

MySQL

1

2

3

4

5

6

7

CREATETABLEIF NOT EXISTS`ci_sessions`(

`id`varchar(40)NOT NULL,

`ip_address`varchar(45)NOT NULL,

`timestamp`int(10)unsignedDEFAULT0NOT NULL,

`data`blobNOT NULL,

KEY`ci_sessions_timestamp`(`timestamp`)

);

The one above is an sql command. You can simply copy it and paste it using PHPMyAdmin’s interface. I sure hope you know how to create a table in MySQL…

Now, after we’ve created the table, if we again visit our homepage from our browser we should be greeted by the CodeIgniter.

Great… We finished setting up the sessions. Now to the Ion Auth library.

We need to put this in our application/language/english directory. If you’ve set another language for your application inside the config.php file, you should copy that language in the appropriate language directory.

libraries/Ion_auth.php and libraries/Bcrypt.php

We should copy these too, inside application/libraries directory.

models/ion_auth_model.php

Copy this file inside the application/models directory. Make sure you copy it by renaming the file so that it starts with upper-case letter: Ion_auth_model.php

Once we did all the copy/pasting we need to set up the tables needed for Ion Auth to work. In order to do this look for a directory named “sql” inside the library’s zip archive. In there we will find an sql command for creating the tables necessary for the library: ion_auth.sql.

Configuring the Ion Auth library

Now that we’ve imported the files and created the tables, we need to configure the library. To do this, we go to our application/config/ion_auth.php.

What should we change here?

We can change $config[‘site_title’], the $config[‘admin_email’] (preferably with a valid email).

Now the important parts.

Identity

Identity is simply asking you what should be used as identity when someone logs in. Some people prefer to use an username, other people prefer email, other people prefer some other type of uniquely identifying value like social security number, badge number, etc. If for example you want to use a social security number in order to identify the users, you should create another field in the “users” table, let’s say “security_number”. Once you’ve done this, you should also change the $config[‘identity’] = ‘security_number’.

For this tutorial we will use username as identity column: $config[‘identity’] = ‘username’.

Email activation

You want Ion Auth to send an email activation code to make sure that the user wants to create the account? Then set this to true.

Manual activation

You want only administrators to activate accounts? Then set this to true.

Remember users

You want to use a “Remember me” on your login form? Set this to true.

Track login attempts

If you want to make sure that on failed login the authentication system remembers how many failed logins have been on a particular account, you should set $config[‘track_login_attempts’] to true. If you set this to true, you should also take a look at $config[‘maximum_login_attempts’] which will lock out an account if the maximum of login attempts has been reached. The lockout period is stated in the $config[‘lockout_time’] (in seconds). That means that after the maximum login attempts has been reached the user is allowed to try one more time every 600 seconds.

Use CI email

As a personal preference I would set $config[‘use_ci_email’] to true;

For now, everything else seems ok.

Create the User controller

We will create a bare-bone controller named User.php inside application/controllers. As you will see, we will load the Ion Auth library inside the constructor in order to use its methods:

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

<?php

defined('BASEPATH')ORexit('No direct script access allowed');

classUserextendsMY_Controller{

function__construct()

{

parent::__construct();

$this->load->library('ion_auth');

}

publicfunctionindex()

{

$this->load->view('welcome_message');

}

publicfunctionlogin()

{

echo'Here we will make the login form';

}

publicfunctionlogout()

{

echo'here we will do the logout';

}

}

We will also need to create a controller that will be accessible only to those users that are logged in. So let’s create a Dashboard.php file inside our application/controllers. This will extend a controller named Auth_Controller:

PHP

1

2

3

4

5

6

7

8

9

10

<?php

defined('BASEPATH')ORexit('No direct script access allowed');

classDashboardextendsAuth_Controller{

publicfunctionindex()

{

echo'Hello from the dashboard';

}

}

The Auth_Controller that we will create soon will be verifying if the users are logged in. If they aren’t, they will be redirected to the login() method of the User controller.

What we’ve done here is some basic templating for our pages. Also we’ve passed some default data that will be passed to the views like the page title and the page description. Now, if some controller extends the MY_Controller, he will pass the data to the views by using $this->data[‘variable_name’], and will render the page by calling $this->render(‘the_view’). Simple enough.

Now we will need to create the Auth_Controller. We will create the class in the same MY_Controller.php file, just after the definition of the MY_Controller class.

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

classAuth_ControllerextendsMY_Controller{

function__construct(){

parent::__construct();

$this->load->library('ion_auth');

if($this->ion_auth->logged_in()===FALSE)

{

redirect('user/login');

}

}

protectedfunctionrender($the_view=NULL,$template='auth_master')

{

parent::render($the_view,$template);

}

}

As you can see, Auth_Controller will take over the data from the MY_Controller. Also, you may observe the fact that inside the constructor, the class verifies that the user is logged in by using the Ion Auth method logged_in() (more on this method can be found here: http://benedmunds.com/ion_auth/#logged_in). If the user is not logged in, he is redirected to the user/login page.

Now let’s visit the dashboard (“http://localhost/dashboard”). If everything went ok we should be automatically redirected to the “login form”… or at least to what will be holding the login form.

Cool… So far so good.

Creating the basic template views

But how do we create the login form? As you can see from our MY_Controller, we created some render() method that will create the views. Also, the default template is named “public_master_view” and is called from the application/views/templatesdirectory. So let’s create one.

We will save a public_master_view.php file inside views/templates directory (which we will create…). The public master view will simply call a public header view and a public footer view from templates/parts directory:

…and the public_master_footer_view.php inside the same application/views/templates/_parts directory:

PHP

1

2

3

4

5

6

7

8

9

<?phpdefined('BASEPATH')ORexit('No direct script access allowed');?>

<footer>

<div class="container">

<pclass="footer">Pagerenderedin<strong>{elapsed_time}</strong>seconds.<?phpecho(ENVIRONMENT==='development')?'CodeIgniter Version <strong>'.CI_VERSION.'</strong>':''?></p>

</div>

</footer>

<?phpecho$before_closing_body;?>

</body>

</html>

We also need to create the template views for the authenticated users. So simply duplicate auth_master_view.php and replace the names of the header and footer views with auth_master_header_view.php and respectively auth_master_footer_view.php.

PHP

1

2

3

4

<?phpdefined('BASEPATH')ORexit('No direct script access allowed');

$this->load->view('templates/_parts/auth_master_header_view');?>

<?phpecho$the_view_content;?>

<?php$this->load->view('templates/_parts/auth_master_footer_view');?>

Also, duplicate the template header view and template footer view in templates/_parts changing the names accordingly to auth_master_header_view.php and auth_master_footer_view.php.

! YOU MIGHT CRY: “Why all this??? I only wanted to implement Ion Auth… Not all this templating stuff. But it is my opinion that you need to have a good start when working on a project. Some sort of good practice (“good practice” as I view it…).

Cool… We’re done.

Now let’s see if it all works. Inside the User.php we created a login() method. Let’s change it so that it will use the render() method we’ve defined in the MY_Controller.

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

<?php

defined('BASEPATH')ORexit('No direct script access allowed');

classUserextendsMY_Controller{

function__construct()

{

parent::__construct();

$this->load->library('ion_auth');

}

publicfunctionindex()

{

$this->load->view('welcome_message');

}

publicfunctionlogin()

{

$this->data['message']='here will be the login form';

$this->render('user/login_view');

}

publicfunctionlogout()

{

echo'here we will do the logout';

}

}

Now we need to create the view that will output that message. So we will create a login_view.php inside application/views/user directory. Why a user directory? It’s best to have views organised by the controllers names (in my opinion…). This way you will find them faster.

PHP

1

2

3

4

<?phpdefined('BASEPATH')ORexit('No direct script access allowed');?>

<div class="container">

<?phpecho$message;?>

</div>

Now, if we visit the same page on our browser we will see that the header and footer was added to our page. Cool. Let’s work on our login() method now…

The login() method

The login method will use Ion Auth methods that will try to log the users and redirect them to the dashboard page.

As I said, we will use “username” as main identity column and the password.

We should first set our form validation rules. For this we will use the native form validation library.

Now let’s modify the login_view.php so that it will have the form. As you can see, inside the login() method we already loaded the native form helper before calling the render() method.

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?phpdefined('BASEPATH')ORexit('No direct script access allowed');?>

<div class="container">

<h1>Loginin</h1>

<?php

echoform_open();

echoform_label('Username:','username').'<br />';

echoform_error('username');

echoform_input('username').'<br />';

echoform_label('Password:','password').'<br />';

echoform_error('password');

echoform_password('password').'<br />';

echoform_checkbox('remember','1',FALSE).' Remember me<br />';

echoform_submit('submit','Log In');

echoform_close();

?>

</div>

As you can see… no css styling… I will leave this to you… But for now, let’s log in. WAIT!!! What username and password should we use to log in? Well… Ion Auth has a default username and password to get us started:

Username: administrator Password: password

Keep in mind that you should change these credentials… on another episode of my series…

Now, if we log in with these credentials we should be taken to the dashboard. But what if we’ve tried to log in with wrong credentials? How would we know with no warning on our login form? If you look back to our login() method we created a session variable named ‘auth_message’ in case the login credetials were bad or some sort of error took place. So why not output this session variable just before the login form. Let’s see the login_view again:

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<?phpdefined('BASEPATH')ORexit('No direct script access allowed');?>

<div class="container">

<?php

echoisset($_SESSION['auth_message'])?$_SESSION['auth_message']:FALSE;

?>

<h1>Loginin</h1>

<?php

echoform_open();

echoform_label('Username:','username').'<br />';

echoform_error('username');

echoform_input('username').'<br />';

echoform_label('Password:','password').'<br />';

echoform_error('password');

echoform_password('password').'<br />';

echoform_checkbox('remember','1',FALSE).' Remember me<br />';

echoform_submit('submit','Log In');

echoform_close();

?>

</div>

Cool.

The logout() method

Let’s log out. How we do this? We change the logout() method of our User controller:

PHP

1

2

3

4

5

publicfunctionlogout()

{

$this->ion_auth->logout();

redirect('user/login');

}

Yes… it’s that simple…

Now let’s add a link to the logout() method on all the pages that belong to the authenticated pages? That means we go to our templates/_parts/auth_master_header_view.php and add a link there.