CREATE LOGIN FORM USING CODEIGNITER FRAMEWORK
CREATE LOGIN FORM IN CODEIGNITER
Date :03/04/2019
In this article, we will help you in creating a simple login form using codeigniter framework.
Pre-requesties
we have to create database and table. Firstly, create database and name it as ‘form’. similarly, create a table named ‘register’.
Database Config Settings
In order to connect with the database, the below instructions must be followed.
The file is located at application/config/database.php.
$db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'form', '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 );
Controller (application/controllers/Welcome.php)
Controller is a process which get the input request from user, communicates with Model and then loads appropriate Views file.
In the below Controller class, we have functions for different tasks.
=>load_login()- This function load the login page.
=>login_validate()-Fetch the data and pass the values to model.
public function load_login() { $this->load->view('login.php'); } public function login_validate() { $this->load->library('form_validation'); $this->form_validation->set_rules('firstname', 'first_name', 'required'); $this->form_validation->set_rules('password', 'password', 'required'); if ($this->form_validation->run()) { $username=$this->input->post('firstname'); $password=$this->input->post('password'); $this->load->model('Insert_model'); if($var1=$this->Insert_model->check_login($username,$password)) { echo "logged in successfully"; } else { $this->session->set_flashdata('error','Invalied username and password'); redirect('Welcome/load_login'); } } else { echo json_encode($this->form_validation->error_array()); $this->load->view('login.php'); } }
Model (application/models/Insert_model.php)
To check the login,
check_login-> function fetches register item by it’s username and password
public function check_login($username,$password) { $this->db->where('first_name',$username); $this->db->where('password',$password); $query=$this->db->get('register'); if($query->num_rows()>0) { return $query->row(); } else { return false; } }
Index View (application/views/login.php)
This view file is called by load_login() function of our Controller class.
<body> <center> <h5>Login Form</h5> <form role="form" method="post" action="<?php echo site_url('Welcome/login_validate');?>"> <table> <tr><td>user name</td><td><input type="text" name="firstname"></td><br></tr> <tr><td>Password</td><td><input type="Password" name="password"></td><br></tr> <tr><td><input type = "submit" name="submit"/></td></tr> </table> </form> </center>
Conclusion:
If you enter the correct login details it will display the message like “logged in successfully”. Otherwise it will show as “Invalid username and password”. That’s it.
Thanks for using pheonixsolutions.
You find this tutorial helpful? Share with your friends to keep it alive.