http://www.developer.com/http://www.developer.com/lang/php/codeigniter-create-your-own-php-registration-system.html
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 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. 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. 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. 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 Notice that we are going to capture user input using the method Finally, we insert the data into the table 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 Now to set validation, use the following line of code: Where 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: Let's review what the above code snippet does. We first verify the form validation using the method If your application is going to use the database more often, then you can load the library by specifying it into the That should be all you need to do to register a new user in your Web application. 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: 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: 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. Finally, we need a way to log out users. We can destroy the current session by calling the 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.
CodeIgniter: Create Your Own PHP Registration System
April 16, 2012
form, url, validation, etc. It does not include plugins or functionality modules like other frameworks or CMSes.Create a Database to Store User Information
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 ; Create User Signup Form
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; } $this->input->post. This takes care of the sanitization process and secures your script from SQL injection(s).ci_users using the active record wrapper method $this->db->insert('table_name',array_of_column_and_values);application/config/autoload.php. $this->load->library('form_validation'); $this->form_validation->set_rules('first_name', 'Name', 'trim|required');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. 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(); } $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.autoload.php file. Finally, we load the model and call the create_user which we discussed above.Create a Login Process
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; } } if(is_array($user) &z& !empty($user)) { $user_data = array('is_logged_in' => 1,'user_email' => $user['user_email']);
//set the session information
$this->session->set_userdata($user_data);
//write code here to show the success message
redirect(site_url('dashboard')); exit(); } Create a Logout Process
sess_destroy() method of the built-in session library.Conclusion