Open SourceCodeIgniter: Create Your Own PHP Registration System

CodeIgniter: Create Your Own PHP Registration System content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

CodeIgniter is one of the most talked about PHP frameworks. It’s known for its lightweight and minimalist footprint, but also for facilitating a complete MVC pattern. Since it’s a lightweight framework, it does come with some basic helpers like form, url, validation, etc. It does not include plugins or functionality modules like other frameworks or CMSes.

While developing a Web application, you should require a login and registration process as the bare minimum for security. In this article, I explain how you can develop a login authentication and signup system using the CodeIgniter framework. I am assuming that you know the basic file / folder arrangement of the CodeIgniter framework.

Create a Database to Store User Information

The following table in MySQL will store all the information related to your users. It only acquires some basic information; you can extend it as desired.

 CREATE TABLE IF NOT EXISTS `ci_users` ( `id_users` bigint(20) NOT NULL AUTO_INCREMENT, `user_login` varchar(255) NOT NULL, `user_pass` varchar(32) NOT NULL, `user_email` varchar(255) NOT NULL, `first_name` varchar(100) NOT NULL, `last_name` varchar(100) NOT NULL, PRIMARY KEY (`id_users`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 

You can copy and paste the above SQL statement to create the users table. Now that we have created the database, we should create the forms.

Create User Signup Form

Since this is going to be simple HTML, I will not write the code here. Be sure that your form action should point to users/register route, such that the request reaches to the users controller and then executes the function “register.”

After creating the form, we need to write the “Model” logic so that we can store the registration information of the user into the database. I am going to create the method create_user inside the “Users” model. The create_user method will have the following code:

 function create_user(){ $first_name = $this->input->post('first_name'); $last_name = $this->input->post('last_name'); $username = $this->input->post('user_login'); $eml = $this->input->post('email_address'); $clear_pass = $this->input->post('password'); $member_data = array( 'user_login' => $username, 'user_pass' => md5($clear_pass), 'user_email' => $eml, 'first_name' => $first_name, 'last_name' => $last_name ); $insert = $this->db->insert('ci_users', $member_data); return $insert; } 

Notice that we are going to capture user input using the method $this->input->post. This takes care of the sanitization process and secures your script from SQL injection(s).

Finally, we insert the data into the table ci_users using the active record wrapper method $this->db->insert('table_name',array_of_column_and_values);

Now that we have the Model and View ready, we need to write controller logic to connect these two so that we can successfully render the registration form and store the data. We are also going to put in validation logic, using CodeIgniter’s built-in validation library, inside the controller itself.

I am assuming that you know how to create controllers in CodeIgniter, so I will discuss the parts of controller methods which matter the most.

First of all, as discussed, let’s load the form validation library using the following line of code. You can autoload this library by specifying its name in the autoload.php located inside application/config/autoload.php.


Now to set validation, use the following line of code:

$this->form_validation->set_rules('first_name', 'Name', 'trim|required');

Where first_name is the name of the HTML element, Name is the human readable name for the element, and trim|required is the validation rules for the element.

You can specify various other rules, and you can define custom validation rules using the callback approach. For more information on validation, check the documentation.

To check if the validation was successful, we use the following code block:

 if($this->form_validation->run() == FALSE) { 
//not validated - reload the view and display errors
$this->load->view('signup'); } else { $this->load->database();
//load users_mode defined in modes/uses_model.php
//create user
$this->users_model->create_user(); }

Let’s review what the above code snippet does. We first verify the form validation using the method $this->form_validation->run(). If it returns true, then we are good. Otherwise, we display errors in the view. If everything was input correctly, we should create the new user. Note that I have created the database connection manually.

If your application is going to use the database more often, then you can load the library by specifying it into the autoload.php file. Finally, we load the model and call the create_user which we discussed above.

That should be all you need to do to register a new user in your Web application.

Create a Login Process

Next, we should provide a way to log in these registered users. The process is going to be the same. We create the View, then Model and finally do the processing inside the Controller.

In the model, after sanitizing the user input (as stated above), we can query the database using the following method:

 function validate_user() { $this->db->select('*'); $this->db->limit(1); $this->db->where('user_email',$user_login); $this->db->where('user_pass', md5($password)); $this->db->from('ci_users'); if($query->num_rows == 1) { $data = $query->row_array(); if($data['user_login'] == $user_login) return $data; else return false; } } 

The above method is simple to understand. I have split the query to make it clearer. If the number of rows is 1, then we validate user, otherwise we do not. We return the complete user information to the controller where we are going to create session information. Let’s see how:

 if(is_array($user) &z& !empty($user)) { $user_data = array('is_logged_in' => 1,'user_email' => $user['user_email']); 
//set the session information
//write code here to show the success message
redirect(site_url('dashboard')); exit(); }

I have redirected data to the dashboard, where we are going to show user-related information. Only valid users are allowed to enter; the rest all are redirected to the login page.

Create a Logout Process

Finally, we need a way to log out users. We can destroy the current session by calling the sess_destroy() method of the built-in session library.


That’s it. Using the above approach, you can set up a login and registration system using CodeIgniter. Using this as base and extend the system as needed.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories