简体   繁体   中英

How to login in codeigniter?

I have syntax, I think the syntax already correct but when I run in browser the login not enter to form the success page. the login just repeats in form_login . if I input username and password wrong are the same nothing notification error. what's wrong the syntax I made?

this is the controller

function __construct()
{
    parent::__construct();
    $this->load->model('account/user_model');
    //        $this->load->helper('url');
    //        $this->load->helper('form');
    $this->load->library('form_validation');

    //tambahin library session supaya mau jalan
    $this->load->library('session');
    //atau di taruh di autoload
}

//melihat halaman login
public function index()
{
    $this->load->view('account/form_login');
}

//memeriksa keberadaan akun username
public function login()
{
    $username = $this->input->post('username','true');
    $password = $this->input->post('password','true');

    $temp_account = $this->user_model->check_user_account($username,$password)->row();
    //check account
    $num_account = count($temp_account);

    $this->form_validation->set_rules('username','username','required');
    $this->form_validation->set_rules('password','password','required');
    if($this->form_validation->run()==FALSE)
    {
        $this->load->view('account/form_login');
    }
    else {
        if ($num_account > 0) {
            //kalau ada set session
            $array_items = array(
                'id_user' => $temp_account->id_user,
                'username' => $temp_account->username,
                'logged_in' => true
            );
            $this->session->set_userdata($array_items);
            redierct(site_url('account/success_page'));
        } else {
            //kalau ga ada diredirect lagi ke halaman login
            $this->session->set_flashdata('notification', 'Peringatan : Username dan Password tidak cocok');
            redirect(site_url('account'));
        }
    }
}
public function view_success_page()
{
    $logged_in = $this->session->userdata('logged_in');
    if(!$logged_in)
    {
        redirect(site_url('account'));
    }
    $this->load->view('account/success_page');
}

this is html login

<?php echo validation_errors();?>
    <p style="color:red"><?php echo $this->session->flashdata('notification')?></p>
    <?php echo form_open('account/login')?>
        Username : <input typer="text" name="username" value="<?php echo set_value('username')?>"/>
        <br>
        <br>
        Password : <input type="password" name="password" value="<?php echo set_value('password')?>"/>
        <br>
        <br>
        <input type="submit" name="masuk" value="LOGIN"/>
        <br>

this is the models

function check_user_account($username,$password)
{
    $this->db->select('*');
    $this->db->from('user');
    $this->db->where('username',$username);
    $this->db->where('password',$password);
    return $this->db->get();
}
//mengambil data user tertentu
function get_user($id_user)
{
    $this->db->select('*');
    $this->db->from('user');
    $this->db->where('id_user',$id_user);
    return $this->db->get();
}

use Ion Auth 3 library Makes it easier for you

https://github.com/benedmunds/CodeIgniter-Ion-Auth

But:

public function login()
{
    $this->form_validation->set_rules('username','username','required');
    $this->form_validation->set_rules('password','password','required');
    if($this->form_validation->run()==true)
    {
    $username = $this->input->post('username','true');
    $password = $this->input->post('password','true');

    $temp_account = $this->user_model->check_user_account($username,$password);
    //check account
        if ($temp_account) {
            //kalau ada set session
            $array_items = array(
                'id_user' => $temp_account->id_user,
                'username' => $temp_account->username,
                'logged_in' => true
            );
            $this->session->set_userdata($array_items);
            redierct(site_url('account/success_page'));
        } else {
          //kalau ga ada diredirect lagi ke halaman login
           $this->session->set_flashdata('notification', 'Peringatan : Username dan Password tidak cocok');
            redirect(site_url('account'));
        }
}
    else {
 $this->load->view('account/form_login');
    }
}

models:

function check_user_account($username,$password)
{
    $this->db->select('*');
    $this->db->from('user');
    $this->db->where('username',$username);
    $this->db->where('password',$password);
    $q = $this->db->get();
        if ($q->num_rows() > 0) {
            return $q->row();
        } else
            return FALSE;
}

I'm sorry I didn't have enough time to test, but I think it's working right now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM