CodeIgniter creates a simple registration system

Source: Internet
Author: User
Tags autoload md5 php framework php website trim codeigniter

CodeIgniter is an application development framework and toolkit for PHP website developers. It provides a rich set of standard libraries and simple interfaces and logical structures, so that developers can perform project development more quickly. CodeIgniter can reduce the amount of code writing and devote your energy to the creative development of projects.
CodeIgniter was developed by RickEllis, CEO of Ellislab. Its core framework is specially compiled for this program, while many other class libraries, auxiliary functions and subsystems come from the content management system ExpressionEngine compiled by RickEllis and PaulBurdick. Inspired by RubyonRails, we have created a PHP framework and introduced the concept of the framework into the general consciousness of the network community.

In this article, I will introduce how to use the CodeIgniter framework to develop a logon authentication and registration system.
Create a database to store user information
The following table stores all information related to MySQL users. It only obtains some basic information and can be extended at will.

The code is as follows: Copy code

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 a user logon table
Since this will be simple HTML, I will not write code here. Make sure that the form action points to the user/registration route. Such a request is sent to the user's controller and then the function "register" is executed"

After creating a table, we need to write the "model" logic so that we can store the user registration information in the database. I want to go to the "user" mode in the create_user method. The create_user method of returns the following code:

The code is as follows: Copy 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 _ address' => $ eml, 'First _ name' => $ first_name, 'Last _ name' => $ last_name); $ insert = $ this-> db-> insert ('ci _ users ', $ member_data); return $ insert ;}

Now that we have model and view preparation, we need to write these two controller logical connections so that we can successfully render the registration form and store data. We also intend to use the verification logic with the built-in verification library of CodeIgniter, the controller itself in it.

I suppose you know how to create the controller in CI, so I will discuss the control method, the most important part.

First, let's use the following line of code to load the form verification Library. The application/config/autoload. php autoload. php in the library can be automatically loaded by specifying its name.

The code is as follows: Copy code

$ This-> load-> library ('form _ validation ');

For verification, you can use the following line of code:

The code is as follows: Copy code

$ This-> form_validation-> set_rules ('First _ name', 'name', 'trim | required ');


Where FIRST_NAME is the name of the HTML element, the name is the name of a human-readable element, trim | verification rules for the required element.

You can specify other rules. You can use the callback method to define custom verification rules. For more information, verify and check documents.

To check if the verification succeeds, we use the following code block:


The code is as follows: Copy code
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 the code snippet above. First, verify the form verification. Use $ this-> form_validation-> run (). If true is returned, it is good. Otherwise, an error is displayed in the view. If everything is entered correctly, we should create a new user. Please note that I manually create a database connection.

If your application uses the database more frequently, you can load the database by specifying the autoload. Php file. Finally, our model loads and calls the create_user we discussed above.

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

Create a logon process
Next, we should provide a method to log on to these registered users. The process is the same. We create a view, and the model and controller are finally processed internally.

User input after disinfection (as shown above). In the model, we can query the database and use the following method:


The code is as follows: Copy code
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 easy to understand. I have already queried the split to make it clearer. If the number of rows is 1, we verify the user, otherwise we do not know. We return the complete user information controller, the session information we want to establish. Let's take a look:

The code is as follows: Copy code

If (is_array ($ user )&&! Empty ($ user) {$ user_data = array ('is _ logged_in '=> 1, 'User _ mail' => $ 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 ();}

I have redirected the data to the dashboard. Here we want to tell the user the relevant information. Only valid users are allowed to enter, and all others are redirected to the logon page.

Create a logout process
Finally, we need a user logout method. By calling the built-in sess_destroy () method, we can destroy the current session.

That's it. With the above method, you can set up a login and registration system to use CodeIgniter. Based on this, the system is extended as needed.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.