mickey mickey - 2 months ago 35
HTML Question

adding login functionality to my project in codeigniter

I have registeration form which redirects to listing page. Now i want to add login functionality to my page and after login my page should show respective users form autofilled. But i didnt get how to use it please help.

controller

function login(){
$this->load->view('login_form.php');
}
function check_login(){
if($_POST) {
$result = $this->Student_info_model->validate_user($_POST);
if(!empty($result)) {
$data = ['id' => $result->user,'name' => $result->name ];
$this->session->set_userdata($data);
redirect('home');
} else {
$this->session->set_flashdata('flash_data', 'Username or password is wrong!');
redirect('Student_info/login');
}
}


Model

public function validate_user($data) {
$this->load->database();
$this->db->where('name', $data['name']);
$this->db->where('password', md5($data['password']));
return $this->db->get('student_info_table')->row();
}


View

<html>
<body>
<form action="<?php echo site_url('Student_info/check_login');?>"method="post">
<label for="username">Username</label>
<input type="text" name="username" /><br>
<label for="password">password</label>
<input type="text" name="password" /><br>
<input type="submit" value="login"/>
</form>




Answer

Paste this code in your controller

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

class Auth extends CI_Controller {

function __construct() {

    parent::__construct();

    $this->load->library('form_validation');
    $this->load->model('auth_model');
}

public function index() {
    if($this->session->userdata('validated') == TRUE){
        redirect( 'welcome' );
    }
    $this->load->view('login');
}
public function message($msg = NULL){
    // Load our view to be displayed
    // to the user
    $data['msg'] = $msg;
    if($msg == NULL){
        $this->load->view('login');
    }
    else{
        $this->load->view('login',$data);
    }
}
public function process(){
    if($this->input->post('submit')){

        $config  = array(
            array(
                'field' => 'username',
                'label' => 'Username',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'trim|required'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->form_validation->run() == FALSE) {
            $data['errors']             = validation_errors();
            $this->load->view('login', $data);
        } else {
                $username       =   $this->security->xss_clean($this->input->post('username'));
                $password       =   $this->security->xss_clean($this->input->post('password'));
                $result         =   $this->auth_model->validate($username,$password);
            if(!$result){
                // If user did not validate, then show them login page again
                $msg = 'Invalid Username or password';
                $this->message($msg);
            }
            else {
                if($this->session->userdata('validated') == true){
                    redirect('welcome');
                 }
            }
        }
    }
    else{
        redirect('auth');
    }
}

} Here is your model

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

class Auth_model extends CI_Model{
function __construct(){
    parent::__construct();

}

function validate($username,$password){
    // Prep the query
    $this->db->where('username', $username);
    // Run the query
    $query = $this->db->get('users')->result();
    if($query){
        foreach($query as $key)
        $hash  = $key->password;

        // Let's check if there are any results
        if (password_verify($password, $hash)) {
        /* Valid */
             $data = array(
                    'id'            =>      $key->id,
                    'username'      =>      $key->username,
                    'validated'     =>      true
            );
            $this->session->set_userdata($data);
            return true;
        } else {
            /* Invalid */
            return false;
        }
    }
    else {
         return false;
    }
}

Here is your Html

<form action="<?php echo base_url().'auth/process' ;?>" role="form" method="post">
                 <?php if(isset ($msg)){ ?>
                    <div class="alert alert-danger">
                    <button class="close" data-close="alert"></button>
                    <?php
                        echo $msg;
                    ?> 
                    </div>
                    <?php }?>
                      <?php
                      if(isset($errors)){
                    ?>
              <div class="alert alert-danger fade in"> <a href="#" class="close" data-dismiss="alert">&times;</a> <strong>Error!</strong> <?php echo ($errors);?> </div>
              <?php }?>

                        <input  name="username"
                        placeholder="User Name" required="" type="text" value="<?php echo set_value('username'
                        );?>">
                    <input  name="password"
                        placeholder="Credentials" required="" type=
                        "password" value="<?php echo set_value('password');?>">


                    <input 
                type="submit" name="submit" value="Sign in" />
            </form>

Let me know if you have any question here

Comments