Dear Reader, let us we know that “How to Create Login and Registration in Codeigniter with Live Demo“ So in this Tutorial, We Learn Step By Step Login and Registration Process.
In any website or Blog Login and Registration are a very important factor which plays a very important role in managing website.In any website, Blog or Portal want to store information about users and when any user want his information in future then we need to save his information for this we need to create Registration.
Login is also very important for authentication of users are valid are not and manage users information user wise.So lets start Login and Registration Creation…
At first we see Live Demo by click on bellow Demo button
We assumed that you have already Configure Codeigniter if you don’t configure then don’t worry, you need to Configure Codeigniter at first here
How to Install CodeIgniter In Windows Operating System Using XAMPP Server
and if you already Configure then you don’t need to Configure again
Step 1:- At very first you need to create users table in your database for storing information of users. for creating table create a database and run bellow SQL query in your database
1 2 3 4 5 6 7 8 9 10 11 |
CREATE TABLE `users` ( `id` int(11) NOT NULL, `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `gender` enum('Male','Female') COLLATE utf8_unicode_ci NOT NULL, `phone` varchar(15) COLLATE utf8_unicode_ci NOT NULL, `created` datetime NOT NULL, `modified` datetime NOT NULL, `status` enum('1','0') COLLATE utf8_unicode_ci NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; |
Step 2:- Now you need to Libraries and helper in autoload.php which is located in
your_project_name\application\config
like
ci_demo\application\config
1 2 3 4 5 |
<?php $autoload['libraries'] = array('session','database'); $autoload['helper'] = array('url','form'); ?> |
Step 3:- At first you need to create Users.php Controller which is located in
your_project_name\application\controllers
like
ci_demo\application\controllers
after creation Users.php Controller paste the below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * User Management class created by Web Preparations */ class Users extends CI_Controller { function __construct() { parent::__construct(); $this->load->library('form_validation'); $this->load->model('user'); } /* * User account information */ public function account() { $data = array(); if($this->session->userdata('isUserLoggedIn')) { $data['user'] = $this->user->getRows(array('id'=>$this->session->userdata('userId'))); //load the view $this->load->view('users/header'); $this->load->view('users/account', $data); $this->load->view('users/footer'); } else { redirect('users/login'); } } /* * User login */ public function login() { $data = array(); if($this->session->userdata('success_msg')) { $data['success_msg'] = $this->session->userdata('success_msg'); $this->session->unset_userdata('success_msg'); } if($this->session->userdata('error_msg')) { $data['error_msg'] = $this->session->userdata('error_msg'); $this->session->unset_userdata('error_msg'); } if($this->input->post('loginSubmit')) { $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); $this->form_validation->set_rules('password', 'password', 'required'); if ($this->form_validation->run() == true) { $con['returnType'] = 'single'; $con['conditions'] = array( 'email'=>$this->input->post('email'), 'password' => md5($this->input->post('password')), 'status' => '1' ); $checkLogin = $this->user->getRows($con); if($checkLogin) { $this->session->set_userdata('isUserLoggedIn',TRUE); $this->session->set_userdata('userId',$checkLogin['id']); redirect('users/account/'); } else { $data['error_msg'] = 'Wrong email or password, please try again.'; } } } //load the view $this->load->view('header'); $this->load->view('users/login', $data); $this->load->view('footer'); } /* * User registration */ public function registration() { $data = array(); $userData = array(); if($this->input->post('regisSubmit')) { $this->form_validation->set_rules('name', 'Name', 'required'); $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check'); $this->form_validation->set_rules('password', 'password', 'required'); $this->form_validation->set_rules('conf_password', 'confirm password', 'required|matches[password]'); $userData = array( 'name' => strip_tags($this->input->post('name')), 'email' => strip_tags($this->input->post('email')), 'password' => md5($this->input->post('password')), 'gender' => $this->input->post('gender'), 'phone' => strip_tags($this->input->post('phone')) ); if($this->form_validation->run() == true) { $insert = $this->user->insert($userData); if($insert) { $this->session->set_userdata('success_msg', 'Your registration was successfully. Please login to your account.'); redirect('users/login'); } else { $data['error_msg'] = 'Some problems occured, please try again.'; } } } $data['user'] = $userData; //load the view $this->load->view('header'); $this->load->view('users/registration', $data); $this->load->view('footer'); } /* * User logout */ public function logout() { $this->session->unset_userdata('isUserLoggedIn'); $this->session->unset_userdata('userId'); $this->session->sess_destroy(); redirect('users/login/'); } /* * Existing email check during validation */ public function email_check($str) { $con['returnType'] = 'count'; $con['conditions'] = array('email'=>$str); $checkEmail = $this->user->getRows($con); if($checkEmail > 0) { $this->form_validation->set_message('email_check', 'The given email already exists.'); return FALSE; } else { return TRUE; } } } |
Step 4:- Now create User.php Model which is located in
your_project_name\application\models
like
ci_demo\application\models
after the creation of User.php Model paste the below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class User extends CI_Model{ function __construct() { $this->userTbl = 'users'; } /* * get rows from the users table */ function getRows($params = array()) { $this->db->select('*'); $this->db->from($this->userTbl); //fetch data by conditions if(array_key_exists("conditions",$params)) { foreach ($params['conditions'] as $key => $value) { $this->db->where($key,$value); } } if(array_key_exists("id",$params)) { $this->db->where('id',$params['id']); $query = $this->db->get(); $result = $query->row_array(); } else { //set start and limit if(array_key_exists("start",$params) && array_key_exists("limit",$params)) { $this->db->limit($params['limit'],$params['start']); } elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)) { $this->db->limit($params['limit']); } $query = $this->db->get(); if(array_key_exists("returnType",$params) && $params['returnType'] == 'count') { $result = $query->num_rows(); } elseif(array_key_exists("returnType",$params) && $params['returnType'] == 'single') { $result = ($query->num_rows() > 0)?$query->row_array():FALSE; } else { $result = ($query->num_rows() > 0)?$query->result_array():FALSE; } } //return fetched data return $result; } /* * Insert user information */ public function insert($data = array()) { //add created and modified data if not included if(!array_key_exists("created", $data)) { $data['created'] = date("Y-m-d H:i:s"); } if(!array_key_exists("modified", $data)) { $data['modified'] = date("Y-m-d H:i:s"); } //insert user data to users table $insert = $this->db->insert($this->userTbl, $data); //return the status if($insert) { return $this->db->insert_id();; } else { return false; } } } |
Step 5:- Now create users folder in views which is located in
your_project_name\application\views
like
ci_demo\application\views
Create registration.php in users folder and paste the below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
<!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="<?php echo base_url();?>assets/css/bootstrap.min.css"> <link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link href="<?php echo base_url(); ?>assets/css/style.css" rel='stylesheet' type='text/css' /> <script src="<?php echo base_url();?>assets/js/jquery.min.js"></script> <script src="<?php echo base_url();?>assets/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2 class="alert alert-info">User Registration</h2> <form action="" method="post"> <div class="form-group"> <input type="text" class="form-control" name="name" placeholder="Name" required="" value="<?php echo !empty($user['name'])?$user['name']:''; ?>"> <?php echo form_error('name','<span class="help-block">','</span>'); ?> </div> <div class="form-group"> <input type="email" class="form-control" name="email" placeholder="Email" required="" value="<?php echo !empty($user['email'])?$user['email']:''; ?>"> <?php echo form_error('email','<span class="help-block">','</span>'); ?> </div> <div class="form-group"> <input type="text" class="form-control" name="phone" placeholder="Phone" value="<?php echo !empty($user['phone'])?$user['phone']:''; ?>"> </div> <div class="form-group"> <input type="password" class="form-control" name="password" placeholder="Password" required=""> <?php echo form_error('password','<span class="help-block">','</span>'); ?> </div> <div class="form-group"> <input type="password" class="form-control" name="conf_password" placeholder="Confirm password" required=""> <?php echo form_error('conf_password','<span class="help-block">','</span>'); ?> </div> <div class="form-group"> <?php if(!empty($user['gender']) && $user['gender'] == 'Female'){ $fcheck = 'checked="checked"'; $mcheck = ''; }else{ $mcheck = 'checked="checked"'; $fcheck = ''; } ?> <div class="radio"> <label> <input type="radio" name="gender" value="Male" <?php echo $mcheck; ?>> Male </label> </div> <div class="radio"> <label> <input type="radio" name="gender" value="Female" <?php echo $fcheck; ?>> Female </label> </div> </div> <div class="form-group"> <input type="submit" name="regisSubmit" class="btn btn-success" value="Submit"/> </div> </form> <p class="footInfo">Already have an account? <a href="<?php echo base_url(); ?>users/login">Login here</a></p> </div> </body> </html> |
Step 6:- Create login.php in users folder and paste the below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<!DOCTYPE html> <html lang="en"> <head> <link href="<?php echo base_url(); ?>assets/css/style.css" rel='stylesheet' type='text/css' /> <link rel="stylesheet" href="<?php echo base_url();?>assets/css/bootstrap.min.css"> <link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="<?php echo base_url();?>assets/js/jquery.min.js"></script> <script src="<?php echo base_url();?>assets/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2 class="alert alert-info">User Login</h2> <?php if(!empty($success_msg)){ echo '<p class="statusMsg">'.$success_msg.'</p>'; }elseif(!empty($error_msg)){ echo '<p class="statusMsg">'.$error_msg.'</p>'; } ?> <form action="" method="post"> <div class="form-group has-feedback"> <input type="email" class="form-control" name="email" placeholder="Email" required="" value=""> <?php echo form_error('email','<span class="help-block">','</span>'); ?> </div> <div class="form-group"> <input type="password" class="form-control" name="password" placeholder="Password" required=""> <?php echo form_error('password','<span class="help-block">','</span>'); ?> </div> <div class="form-group"> <input type="submit" name="loginSubmit" class="btn btn-success" value="Submit"/> </div> </form> <p class="footInfo">Don't have an account? <a href="<?php echo base_url(); ?>users/registration">Register here</a></p> </div> </body> </html> |
Step 7:- Create account.php in users folder and paste the below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html> <html lang="en"> <head> <link href="<?php echo base_url(); ?>assets/css/style.css" rel='stylesheet' type='text/css' /> <link rel="stylesheet" href="<?php echo base_url();?>assets/css/bootstrap.min.css"> <link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="<?php echo base_url();?>assets/js/jquery.min.js"></script> <script src="<?php echo base_url();?>assets/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>User Account</h2> <h3>Welcome <?php echo $user['name']; ?>!</h3> <div class="account-info"> <p><b>Name: </b><?php echo $user['name']; ?></p> <p><b>Email: </b><?php echo $user['email']; ?></p> <p><b>Phone: </b><?php echo $user['phone']; ?></p> <p><b>Gender: </b><?php echo $user['gender']; ?></p> </div> </div> </body> </html> |
Step 8:- Create a assets folder in your project and create CSS folder in the assets folder
your_project_name/assets/css
like
ci_demo/assets/css
Now create a style.css file in css folder and paste the below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<style> #header{ background-color: #449D44; color: #fff; border-color: #449D44; } #header a{ color: #fff; } #header a:hover{ background-color: #414cf4; } #footer{ width: 100%; background-color: #000; height: 100px; color: #fff; padding: 30px; } #home-title{ width:90%; margin-left: auto; margin-right: auto; } /* for admin css start */ #user-header{ background-color: #414cf4; color: #fff; border-color: #449D44; } #user-header a{ color: #fff; } #user-header a:hover{ background-color: #449D44; } /* for admin css close */ </style> |
step 9:- Now open your Browser run your project as
localhost/your_project_name/users/registration you can see the result as below snapshot
step 10:- Now fill all required details and click submit button
you can see the result that your registration was successful as below snapshot
step 11:- After Registration for login fill email and password click submit button
step 12:- After Authentication successfully you will be redirected your profile as below snapshot
step 13:- if you want to log out your profile you need to click on Logout you will be redirected login page as you can see in below snapshot
Congratulations you have successfully created Registration and Login in Codeigniter, if you have any query then contact us or comment below, Thanks
10 Comments:
amara wazir November 26, 2018
can i get the source code please
Webpreparations Team November 27, 2018
Yes, please write your email here, thank you for contact us.
amara wazir November 27, 2018
amarairum12@gmail.com .thank for your kind reply
Webpreparations Team November 28, 2018
Please download all file by click on the download button from the last of the Article by near demo button.
Thank you for contact us 🙂
amara wazir November 29, 2018
Thanks A Lot For The Tutorial…Its a very good tutorial for the beginners ..it helped me a lot
Webpreparations Team November 29, 2018
Thank you so much dear Reader for the appreciation.
Pheng December 21, 2018
hi, do i need to submit database connection? When i clicked on register it looks like no response. can you guide me on this please? Thanks
Webpreparations Team December 21, 2018
Hello,
Thank you for contact us, please cheek your database connection in config folder of database.php file. Please put the correct details of database connectivity.
Refaqat July 05, 2019
Please add more codeigniter tutorial it helps alot… Thanks You
WebPreparations Team July 06, 2019
Hello Dear,
Thank you for your appreciation, sure we will add more tutorial, we are always ready to help. 🙂