August 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

CodeIgniter: Create Your Own PHP Registration System

  • April 16, 2012
  • By Sachin Khosla
  • Send Email »
  • More Articles »

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.

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

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
$this->load->model('users_model');
//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.


Tags: PHP, frameworks

Originally published on http://www.developer.com.

Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel