Today we will learn about login and registration in this tutorial. How do we log in and register in PHP Codeigniter?
- First we will go to the database. And create a table.
- Registration will be made after that, we will submit the details of the user in PHP MySQL
- At the time of registration, we will check the email id that this email id is already inserted in the database. If it is inserted, then a message will be sent that email id Already Exist
- Add validation to login and registration
- Convert the user’s simple password with the hash string.
- User login form enter the system
- And the user will store the session
- Create a user profile
- Users will be logged out in a system
1-Table structure for table tblregistration
CREATE TABLE `tblregistration` ( `Id` int(11) NOT NULL, `Fristname` varchar(45) DEFAULT NULL, `Lastname` varchar(45) DEFAULT NULL, `Phoneno` varchar(45) DEFAULT NULL, `gender` varchar(45) DEFAULT NULL, `Emailid` varchar(45) DEFAULT NULL, `Password` varchar(45) DEFAULT NULL, `Create_date` timestamp NULL DEFAULT current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
2-Database configuration
application/config/database.php
$active_group = 'default'; $query_builder = TRUE; $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'student', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
3-Add to Libraries and helper
application/config/Autoload.php
$autoload['libraries'] = array('database','form_validation','session','encryption'); $autoload['helper'] = array('html','url','form','captcha');
4-Add to by Default Controller set
application/config/routes.php
$route['default_controller'] = 'Login'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE;
5-Create a new Registration From(registration.php)
application/views/registration.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title>User Login and Registration With Admin Panel</title> <!-- Custom fonts for this template--> <?php echo link_tag('assets/vendor/fontawesome-free/css/all.min.css');?> <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet"> <!-- Custom styles for this template--> <?php echo link_tag('assets/css/sb-admin-2.min.css');?> </head> <body class="bg-gradient-primary"> <div class="container"> <div class="card o-hidden border-0 shadow-lg my-5"> <div class="card-body p-0"> <!-- Nested Row within Card Body --> <div class="row"> <div class="col-lg-5 d-lg-block"> <img src="<?php echo base_url('assets/img/2.jpg');?>"> </div> <div class="col-lg-7"> <div class="p-5"> <div class="text-center"> <h1 class="h4 text-gray-900 mb-4">Create an Account!</h1> </div> <?php echo $this->session->flashdata('msg');?> <?php echo form_open('Registration',['name'=>'signup','class'=>'user']);?> <div class="form-group row"> <div class="col-sm-6 mb-3 mb-sm-0"> <?php echo form_input(['name'=>'fname','id'=>'fname','class'=>'form-control form-control-user','placeholder'=>'First Name']);?> <span style="color: red;"><?php echo form_error('fname');?></span> </div> <div class="col-sm-6"> <?php echo form_input(['name'=>'lname','id'=>'lname','class'=>'form-control form-control-user','placeholder'=>'Last Name']);?> <span style="color: red;"><?php echo form_error('lname');?></span> </div> </div> <div class="form-group row"> <div class="col-sm-6 mb-3 mb-sm-0"> <?php echo form_input(['name'=>'phone','id'=>'phone','maxlength'=>'10','class'=>'form-control form-control-user','placeholder'=>'Phone Number']);?> <span style="color: red;"><?php echo form_error('phone');?></span> <span id="phone_result"></span> </div> <div class="col-sm-6"> <?php echo form_radio(['name'=>'gender','value'=>'Male']);?>Male <?php echo form_radio(['name'=>'gender','value'=>'Female']);?>Female <?php echo form_radio(['name'=>'gender','value'=>'Transgender']);?>Transgender <span style="color: red;"><?php echo form_error('gender');?></span> </div> </div> <div class="form-group"> <?php echo form_input(['name'=>'email','id'=>'email','class'=>'form-control form-control-user','placeholder'=>'Email Address']);?> <span style="color: red;"><?php echo form_error('email');?></span> <span id="email_result"></span> </div> <div class="form-group row"> <div class="col-sm-6 mb-3 mb-sm-0"> <?php echo form_password(['name'=>'Password','id'=>'Password','class'=>'form-control form-control-user','placeholder'=>'Password']);?> <span style="color: red;"><?php echo form_error('Password');?></span> </div> <div class="col-sm-6"> <?php echo form_password(['name'=>'cpassword','id'=>'cpassword','class'=>'form-control form-control-user','placeholder'=>'Repeat Password']);?> <span style="color: red;"><?php echo form_error('cpassword');?></span> </div> </div> <?php echo form_submit(['name'=>'submit','id'=>'submit','value'=>'Register Account','class'=>'btn btn-primary btn-user btn-block']);?> <hr> <?php echo form_close();?> <hr> <div class="text-center"> <a class="small" href="<?php echo site_url('Login');?>">Already have an account? Login!</a> </div> </div> </div> </div> </div> </div> </div> <!-- Bootstrap core JavaScript--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <script src="<?php echo base_url('assets/jquery/jquery.min.js');?>"></script> <script src="<?php echo base_url('assets/bootstrap/js/bootstrap.bundle.min.js');?>"></script> <!-- Core plugin JavaScript--> <script src="<?php echo base_url('assets/vendor/jquery-easing/jquery.easing.min.js');?>"></script> <!-- Custom scripts for all pages--> <script src="<?php echo base_url('assets/js/sb-admin-2.min.js');?>"></script> </body> </html> <script> $(document).ready(function(){ $('#email').change(function(){ var email = $('#email').val(); if(email != '') { $.ajax({ url:"<?php echo base_url(); ?>Registration/check_email_avalibility", method:"POST", data:{email:email}, success:function(data){ //alert(data); $('#email_result').html(data); //alert(data); } }); } }); }); </script> <script> $(document).ready(function(){ $('#phone').change(function(){ var phone = $('#phone').val(); if(phone != '') { $.ajax({ url:"<?php echo base_url(); ?>Registration/check_phone_avalibility", method:"POST", data:{phone:phone}, success:function(data){ //alert(data); $('#phone_result').html(data); //alert(data); } }); } }); }); </script>
6-Create a Model(Registration_model.php)
application/models/Registration_model.php
<?php Class Registration_model extends CI_Model{ public function reg_insert($fname,$lname,$Phoneno,$gender,$email,$password) { $data=array( 'Fristname'=>$fname, 'Lastname'=>$lname, 'Phoneno'=>$Phoneno, 'gender'=>$gender, 'Emailid'=>$email, 'Password'=>$password ); $query = $this->db->insert('tblregistration', $data); if($query){ return $this->db->insert_id(); } else{ return false; } } function is_email_available($email) { $this->db->where('Emailid', $email); $query = $this->db->get("tblregistration"); return $query->result(); } function is_phone_available($phone) { $this->db->where('Phoneno', $phone); $query = $this->db->get("tblregistration"); return $query->result(); } } ?>
7-Create a Controller(Registration.php)
application/controllers/Registration.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Registration extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('Registration_model'); } public function index() { $this->form_validation->set_rules('fname', 'First Name', 'trim|required|min_length[3]|max_length[30]'); $this->form_validation->set_rules('lname', 'Last Name', 'trim|required|min_length[3]|max_length[30]'); $this->form_validation->set_rules('phone', 'Phone', 'required|is_unique[tblregistration.Phoneno]'); $this->form_validation->set_rules('gender', 'radio button', 'required'); $this->form_validation->set_rules('email', 'Email ID', 'trim|required|is_unique[tblregistration.Emailid]'); $this->form_validation->set_rules('Password', 'Password', 'trim|required|min_length[5]|matches[cpassword]'); $this->form_validation->set_rules('cpassword', 'Confrim password', 'trim|required|min_length[5]'); if ($this->form_validation->run() == FALSE) { $this->load->view('registration'); } else{ $firstname=$this->input->post('fname'); $lastname=$this->input->post('lname'); $phone=$this->input->post('phone'); $gender=$this->input->post('gender'); $email=$this->input->post('email'); $password=sha1($this->input->post('Password')); if($this->Registration_model->reg_insert($firstname,$lastname,$phone,$gender,$email,$password)) { $this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully Registered! Please login to access your Profile!</div>'); redirect('Registration'); } else{ $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>'); redirect('Registration'); } } } function check_email_avalibility() { if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) { echo '<label class="text-danger"><span class="glyphicon glyphicon-remove"></span> Invalid Email</span></label>'; echo "<script>$('#submit').prop('disabled',true);</script>"; } else { if($this->Registration_model->is_email_available($_POST["email"])) { echo '<label class="text-danger"><span class="glyphicon glyphicon-remove"></span> Email Already register</label>'; echo "<script>$('#submit').prop('disabled',true);</script>"; } else { echo '<label class="text-success"><span class="glyphicon glyphicon-ok"></span> Email Available</label>'; } } } function check_phone_avalibility() { if(!is_numeric($_POST["phone"])) { echo '<label class="text-danger"><span class="glyphicon glyphicon-remove"></span> Invalid Phone</span></label>'; echo "<script>$('#submit').prop('disabled',true);</script>"; } else { if($this->Registration_model->is_phone_available($_POST["phone"])) { echo '<label class="text-danger"><span class="glyphicon glyphicon-remove"></span> Mobile Already register</label>'; echo "<script>$('#submit').prop('disabled',true);</script>"; } else { echo '<label class="text-success"><span class="glyphicon glyphicon-ok"></span> Mobile No Available</label>'; } } } }
8-Create a new Login From(login.php)
application/views/login.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title>Welcome To Login and Registration</title> <?php echo link_tag('assets/vendor/fontawesome-free/css/all.min.css');?> <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet"> <!-- Custom styles for this template--> <?php echo link_tag('assets/css/sb-admin-2.min.css');?> </head> <body class="bg-gradient-primary"> <div class="container"> <!-- Outer Row --> <div class="row justify-content-center"> <div class="col-xl-10 col-lg-12 col-md-9"> <div class="card o-hidden border-0 shadow-lg my-5"> <div class="card-body p-0"> <div class="row"> <div class="col-lg-6"> <img src="<?php echo base_url('assets/img/1.jpg');?>"> </div> <div class="col-lg-6"> <div class="p-5"> <div class="text-center"> <h1 class="h4 text-gray-900 mb-4">Welcome Back!</h1> </div> <?php echo $this->session->flashdata('msg');?> <?php echo form_open('Login',['name'=>'signupsx','class'=>'user']);?> <div class="form-group"> <?php echo form_input(['name'=>'email','id'=>'email','class'=>'form-control form-control-user','placeholder'=>'Enter Email Address...']);?> <span style="color: red;"><?php echo form_error('email');?></span> </div> <div class="form-group"> <?php echo form_password(['name'=>'password','id'=>'password','class'=>'form-control form-control-user','placeholder'=>'Enter password...']);?> <span style="color: red;"><?php echo form_error('password');?></span> </div> <div class="form-group"> <div class="custom-control custom-checkbox small"> <input type="checkbox" class="custom-control-input" id="customCheck"> <label class="custom-control-label" for="customCheck">Remember Me</label> </div> </div> <?php echo form_submit(['name'=>'submit','id'=>'submit','value'=>'Login','class'=>'btn btn-primary btn-user btn-block']);?> <hr> </form> <hr> <div class="text-center"> <a class="small" href="<?php echo site_url('Registration');?>">Create an Account!</a> </div> </div> </div> </div> </div> </div> </div> </div> </div> </body> </html>
9-Create a Model(Login_model.php)
application/models/Login_model.php
<?php Class Login_model extends CI_Model{ public function checkLogin($email,$password) { $this->db->where('Emailid', $email); $this->db->where('Password', $password); $query = $this->db->get('tblregistration'); if($query!=NULL){ return $query->row(); } } } ?>
10-Create a Controller(Login.php)
application/controllers/Login.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('Login_model'); } public function index() { $this->form_validation->set_rules('email', 'Email ID', 'trim|required'); $this->form_validation->set_rules('password', 'password', 'trim|required'); if ($this->form_validation->run() == FALSE) { $this->load->view('login'); } else{ $email=$this->input->post('email'); $password=sha1($this->input->post('password')); $uresult = $this->Login_model->checkLogin($email,$password); if($uresult) { $this->session->set_userdata('uid',$uresult->Id); $this->session->set_userdata('fname',$uresult->Fristname); $this->session->set_userdata('lname',$uresult->Lastname); $this->session->set_userdata('email',$uresult->Emailid); $this->session->set_userdata('Phoneno',$uresult->Phoneno); $this->session->set_userdata('gender',$uresult->gender); redirect(base_url().'Dashboard'); } else { $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Email id And Password is Invalid!</div>'); redirect('Login'); } } } }
11-Create a new Dashboard From(dashboard.php)
application/views/dashboard.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title>Student Registration</title> <!-- Custom fonts for this template--> <?php echo link_tag('assets/vendor/fontawesome-free/css/all.min.css');?> <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet"> <!-- Custom styles for this template--> <?php echo link_tag('assets/css/sb-admin-2.min.css');?> </head> <body class="bg-gradient-primary"> <div class="container"> <div class="card o-hidden border-0 shadow-lg my-5"> <div class="card-body p-0"> <!-- Nested Row within Card Body --> <div class="row"> <div class="col-lg-5 d-lg-block"> <img src="<?php echo base_url('assets/img/2.jpg');?>"> </div> <div class="col-lg-7"> <div class="p-5"> <div class="text-center"> <h1 class="h4 text-gray-900 mb-4">User Dashboard!---------<a href="<?php echo base_url("Logout");?>">Logout</a></h1> </div> <?php $fname=$this->session->userdata('fname');?> <?php $lname=$this->session->userdata('lname');?> <?php $email=$this->session->userdata('email');?> <?php $Phoneno=$this->session->userdata('Phoneno');?> <?php $gender=$this->session->userdata('gender');?> <hr> <div class="form-group row"> <div class="col-sm-6 mb-3 mb-sm-0"> <label><strong>First Name:-</strong><?php echo $fname;?></label> </div> <div class="col-sm-6"> <label><strong>Last Name:-</strong><?php echo $lname;?></label> </div> <hr> <div class="col-sm-6"> <label><strong>Mobile No:-</strong><?php echo $Phoneno;?></label> </div> <div class="col-sm-6"> <label><strong>Email Id:-</strong><?php echo $email;?></label> </div> <hr> <div class="col-sm-6"> <label><strong>Gender:-</strong><?php echo $gender;?></label> </div> </div> </div> </div> </div> </div> </div> </div> <!-- Bootstrap core JavaScript--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <script src="<?php echo base_url('assets/jquery/jquery.min.js');?>"></script> <script src="<?php echo base_url('assets/bootstrap/js/bootstrap.bundle.min.js');?>"></script> <!-- Core plugin JavaScript--> <script src="<?php echo base_url('assets/vendor/jquery-easing/jquery.easing.min.js');?>"></script> <!-- Custom scripts for all pages--> <script src="<?php echo base_url('assets/js/sb-admin-2.min.js');?>"></script> </body> </html>
12-Create a Controller(Dashboard.php)
application/controllers/Dashboard.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Dashboard extends CI_Controller { function __construct(){ parent::__construct(); if(!$this->session->userdata('uid')) redirect('Login'); } public function index() { $this->load->view('dashboard'); } }
13-Logout Controller(Logout.php)
application/controllers/Logout.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Logout extends CI_Controller { //Validating login function __construct(){ parent::__construct(); if(!$this->session->userdata('uid')) redirect('Login'); } //Function for logout public function index(){ $this->session->unset_userdata('uid'); $this->session->sess_destroy(); return redirect('Login'); } }
How to User Login And Registration Using PHP Codeigniter with source code
Size: 2Mb
Version: 3.11
100 Downloads