In Web Development
0
146 Views

Hello friends, this is my first blog on Codeigniter framework. In this, we are going to make login, signup and user listing system. Firstly, install the Codeigniter framework either in your local server means localhost or in any live server. Also, below are the topics which we will discuss in this section.

  1. Signup System
  2. Login System
  3. User Listing ( Display the registered users)

Configuration Setting

Before start coding, we need to set some configuration settings in our application/config folder open config.php file and do some configuration as shown below.

  • Set base URL

Base URL setting

  • Set index page

Index page configuration

Database setting

Here, we are going to make the connection between our Codeigniter application and Mysqli. In application/config folder open database.php file and also, place the below code in that file.

Database setting

In above code Hostname is  localhost”, database name is test” and username is “root”.

Config (autoload.php)

In this file, we need to load all the libraries and helpers we want. The file path is application/config/autoload.php. Also, below is the example of loading libraries and helpers.

Create Users Table

Before, doing coding first create users table in your database. Also, below is the code for creating table.

Signup System

In signup system first, we need to make a form containing fields like Name, Username, Email and Password. For that purpose, we have already made a form named add.php in appliaction/view folder.

second, we made the file named footer.php where we place our Css and Js files.

Before sending the form data to the controller we need to validate that data through javascript or jquery. Here, first we are using checkonlyletterspace( ) function to check the string that it contains only letters or spacing. Second, we are using uniqueemailForProfile( ) function to check the email that it already exists in the database or not by sending the email to the Home.php controller where,  uniqueemailForProfile( ) function also exists in the controller to check the email in the database. After validating all the data we use ajax to send the form data to the controller named as Home.php. Make folder named assets in the root directory of your application. Copy the below code and paste it into file and save it as signup_validation.js inside assets folder in the root directory like application/assets/validation.php.

At last, create index method that will load the registration page as shown below.

Controller Method for signup system

The controller is the main module or part that connect view to the model part. In controller we made a method named insertData( ). In insertData() method we are calling insert_data model that insert the data into the users table in the database and if the data is correctly inserted in the database it returns success message back to the controller. On the basis of message, we send “0” or “1” to our add.php view file. Create Home.php controller where make class named Home that extends the CI_Controller and place the below code inside that class.

Model Method for Signup System

Here, first we need to make a User_model.php file inside application/model folder and within that file create class named User_model that extends the CI_Model. Also, create insert_data method and paste the below code under that.

In above code we have used two methods first one is insert_data( ) to insert data and the second one is randomUniqueToken( ) to generate 30 digit random number every time when the user wants to register. Here, token variable is used to provide every user a unique number which is then used for security purpose. Once, the data is stored in the database it will return the message according to that user.

Login System

Create login page that have fields email and password. Save that page as login.php in application/view. Also, below is the code for the login form.

In addition, create file that has validation functions to validate email and password. Save this file as login_validation.js in root folder named assets as assets/login_validation.js. Login_validation.js contains uniqueemailForProfile function to check that email exists in the database or not by sending the email to Home.php controller through ajax. Also, below is the login validation code.

In above code, after validating the email and password these data will be sent to the home controller through ajax. Also, that data is used for validating the data stored in the database and insert them if exist.

Controller for login

In home controller, we have created a method named checkPwdAndLogin( ). In checkPwdAndLogin() method we are calling checkPwdAndLogin model that checks the data into the users table in the database and if the data is correctly matched with the stored data in the database it returns data of the user in associative array back to the controller. On the basis of that data we store the data into session and send “0” or “1” to our login.php view file. If we get “1” on login page it means user is authorized and if “0” will get then it means the user does not exist in the database. Also, below is the code for checking the email and password in the database.

Model for login

Create a method in user_model to check whether email and password exist in the database or not, if exist then fetch the details of that user and send it back to the controller. Also, below is the code for the model named checkPwdAndLogin.

In the above code we are checking the email and password into the user table. If exist it means authorized user and return associative array of that user otherwise return null.

User listing System

Create a view named user.php in application/view folder. In this file, we are having foreach loop to fetch the data from JSON response data. Also, below is the code for showing the user details if exist.

In addition, we create controller method named show( ) in Home.php controller.  Also, below is code for show( ) method. The function of show method is to fetch the data from users table and send it to users.php view.

In above code first, we load the user_model file and then call the get_users( ) method to fetch the registered user data. At the end of code we are sending that data to user view.

Model to get the users detail

As we have already created view and controller file. Here, we are talking about the database select query to fetch the users detail on the basis of isActive field name. If, isActive field has value “1” it means the user is active and we get the data of that user. Also, below is the code for model method named get_users( ).

In above code, we are having the condition that isActive field is “1” or not. If “1” then we will get the data as objects.

Important Links

  • http://localhost/test/home/   ( This URL will open add user page as shown below. Here, “test” is your codeigniter application folder name and “home” is your main controller name.)

Conclusion

In this post, we have learned how to make login, signup and user listing form in Codeigniter. In this, we have used validation script to validate text, email and password. So, if you like my post or have any query please comment below in the comment box.

RECOMMENDED POSTS

Start typing and press Enter to search