Project Tutorials
Introduction Directory Structure Project Setup Remove index.php using htaccess Change Default Controller CSS Add in Project Create Login Page Design Validation on Login Login with Database Flashdata on Login Display Username after Login Logout Design Dashboard Page List user data form database Insert Data Flash message show if data inserted Delete data from datebase Edit data from datebase Active Inactive user status Show active users detail on home page Image Upload Edit Image View Image on Home PageLogin with Database
In this tutorial, Is user existed in database. If yes, so it will send you to dashboard other it will show invalid credentials.
Step 1 : Go to your databse (ci_project) and create table (login) in database or run this sql query in your database.
eg :
CREATE TABLE `ci_project`.`login` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(300) NOT NULL , `pass` VARCHAR(100) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
Step 2 : Insert users record in login table.
INSERT INTO `login` (`id`, `name`, `pass`) VALUES (NULL, 'elevenstech', '1234567'), (NULL, 'kailash_singh', '1234567')
Step 3 : In this code, we are passing user details to login model to check user existed or not in databse, if user exist then create a user session and send to dashboard, if not then it will show invalid credentials.
eg :
<?php
class Login extends CI_Controller {
public function index()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('uname','Username','required');
$this->form_validation->set_rules('password','Password','required');
if($this->form_validation->run())
{
//get user details in variables
$user = $this->input->post('uname');
$pass = $this->input->post('password');
//here i am creating a login model
$this->load->model('login_model');
// In this line, we are passing user details to login model to check user existed or not in databse
$login = $this->login_model->login_data($user, $pass);
if($login){
//if user exist then create a user session and send to dashboard
$this->session->set_userdata('id',$login);
return redirect('dashboard');
}
else{
//if not, then it will show invalid credentials.
echo'Invalid credentials';
}
}
else
{
$this->load->view('login');
}
}
}
?>
Step 4 : Create Login Model ( Login_model.php ) in Model folder. In this model, we are going to check user exist in database or not. If user exist then it will return the user's ID otherwise return False.
eg :
Code :
<?php
class Login_model extends CI_Model
{
public function login_data($user, $pass)
{
//here i am fetching data from login table using where condition
$query = $this->db->where(['name'=>$user,'pass'=>$pass])
->get('login');
//get total number of rows
if($query->num_rows() == 1)
{
// return user id if user existed in login table
return $query->row()->id;
}
else
{
// return false if user doen not existed in login table
return FALSE;
}
}
}
?>
Result :
Source Code:
Codeigniter tutorial for beginners
In this project. We are providing you, how to create small project in Codeign....
Source CodeElevenstech Web Tutorials
Elevenstech Web Tutorials helps you learn coding skills and enhance your skills you want.
As part of Elevenstech's Blog, Elevenstech Web Tutorials contributes to our mission of “helping people learn coding online”.
Read More
Newsletter
Subscribe to get the latest updates from Elevenstech Web Tutorials and stay up to date