SignIn By Codeigniter

Auth.php           Controller

<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Auth extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('auth_model');
    }

    public function login()
    {
        if ($this->session->userdata('logged_in') == true) {
            redirect('dashboard');
        }
        if(is_array($this->input->post()) && count($this->input->post())>0){
            $email = $this->input->post('email');
            $password = $this->input->post('password');
            $user_check = $this->auth_model->user_check($email);
         
            if ($user_check) {
                $user_data = $this->auth_model->login_user($email, $password);
                if ($user_data) {
   
                    if($user_data['type'] == 1 ){
                         $session_data_admin = array(
                                            "user_id"       =>  $user_data['id'],
                                            "uuid"          =>  $user_data['uuid'],
                                            "first_name"    =>  $user_data['first_name'],
                                            "last_name"     =>  $user_data['last_name'],
                                            "email"         =>  $user_data['email'],
                                            "type"          =>  $user_data['type'],
                                            "logged_in"     =>  true,
                                        );
                        $this->session->set_userdata($session_data_admin);
                        redirect(base_url() . 'dashboard');
                    }

                } else {
                    $this->session->set_flashdata('error_msg', 'Invalid Password, please try again.');
                    redirect('auth/login');
                }
            } else {
                $this->session->set_flashdata('error_msg', 'Email address not exists in system');
                redirect('auth/login');
            }
        }
        $this->load->view('include/header');
        $this->load->view('auth/login');
        $this->load->view('include/footer');
    }

    public function logout()
    {
        $this->session->sess_destroy();
        redirect('/', 'refresh');
        exit;
    }
}

Auth_model.php                Model

<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Auth_model extends CI_Model
{
    public function user_check($email )
    {
        $this->db->select('*')
            ->from('accounts')
            ->where('email', $email );
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    public function login_user($email , $password)
    {
        //$pass = md5($password);
        $this->db->select('*')
            ->from('accounts')
            ->where('email', $email )
            ->where('password', $password);
         
        if ($query = $this->db->get()) {
            return $query->row_array();
        } else {
            return false;
        }
    }
}

login.php             View 
<div class="container-fluid text-center">   
  <div class="row content">
    <div class="col-sm-4 text-left">
       <h1 class="page-title text-center">Admin Login</h1>
    </div>

    <div class="col-sm-8 text-left">
      <form method="POST" action="<?php echo base_url('Auth/login')?>">
        <?php
            $error_msg=$this->session->flashdata('error_msg');
         if($error_msg){?>
        <div class="alert alert-danger" role="alert">
            <small><?php echo $error_msg; ?></small>
        </div>
        <?php } ?>
        <br>
        <div class="form-group">
            <label for="email">Email</label>
            <input type="text" class="form-control" name="email"
                placeholder="Enter your email">
        </div>
        <div class="form-group">
            <label for="password">Password</label>
            <input type="password" class="form-control" name="password"
                placeholder="Enter your password">
        </div>
        <div class="form-group form-check">
            <input type="checkbox" class="form-check-input" id="remember">
            <label class="form-check-label" for="remember">Remember Me</label>
        </div>
        <button class="btn btn-primary">Submit</button>
      </form>
    </div>
  </div>
</div>

<script>
    window.setTimeout(function() {
        $(".alert").fadeTo(500, 0).slideUp(500, function(){
            $(this).remove();
        });
    }, 3000);
</script>

   
Dashboard.php             Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Dashboard extends CI_Controller {

public function index()
{
$this->load->view('include/header');
        $this->load->view('dashboard/dashboard');
        $this->load->view('include/footer');
}
}

dashboard.php         view

<div class="container-fluid text-center">   
  <div class="row content">
    <div class="col-sm-12">
      <h1>Welcome To Dashboard</h1>
      <p>loged in</p>
      <hr>
    </div>
  </div>
</div>

SQL Script

CREATE TABLE `accounts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uuid` varchar(120) NOT NULL,
  `type` tinyint(2) NOT NULL COMMENT '1=admin',
  `user_name` varchar(50) NOT NULL,
  `first_name` char(40) DEFAULT NULL,
  `last_name` char(40) NOT NULL,
  `email` char(80) NOT NULL,
  `password` char(200) NOT NULL,
  `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1=deleted',
  `status` tinyint(1) NOT NULL COMMENT '0=active, 1=inactive',
  `c_date` datetime NOT NULL,
  `u_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `flag_password` tinyint(4) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)

);

INSERT INTO `accounts` (`id`, `uuid`, `type`, `user_name`, `first_name`, `last_name`, `email`, `password`, `deleted`, `status`, `c_date`, `u_date`, `flag_password`) VALUES
(1, '342923e4-93f8-4f84-88cc-eab781c8ebd8', 1, 'v blog', 'admin', 'admin', 'admin@gmail.com', '123456', 0, 0, '2020-03-06 00:00:00', '2020-03-05 07:30:00', 0);



Comments

Popular posts from this blog

API

Encryption and Decryption By PHP

Seeder