• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Authenticating a User through Login

#1
[eluser]Gwarrior[/eluser]
Referring to this Thread, I suppose I am completely unable to authenticate a login.

I mean, it seemed pretty simple - just query the DB for password where username = suppliedusername, then compare the requested password with the submitted password, but that doesn't seem to be working, at all.

Can someone just lay it out for me, how I am supposed to authenticate a user (I have used PHP for 2 years while NEVER using sessions/cookies for some reason)? Either in layman's, or code-wise.

p.s. Below is my code:

Controller:
Code:
<?php
class Login extends Controller {
    function Login() {
        parent::Controller();
        $this->load->helper('form');
        $this->load->helper('url');
        
        $this->load->scaffolding('users');
    }
        function index() {
         $this->load->database();
         $this->load->model('login_model');
        $this->load->library('session');
        if (isset($_POST['submitted'])) {
        $password = $this->login_model->get_password($_POST['username']);
        if ($password == $_POST['password']) {
            $this->login_model->set_session($_POST['username']);
            redirect('home/success');
        } else {
            redirect('home/failure');
        }
        }
        $this->load->view('login');
    }
    function success() {
        $this->load->library('session');
         $data['username'] = $this->session->userdata('username');
        $this->load->view('success', $data);
    }
    function failure() {
        $this->load->view('failure');
    }
    function testdb() {
        $this->load->scaffolding('users');
    }
}

?>

Model:
Code:
<?php
class Login_model extends Model {
    function Login_model() {
        parent::Model();
    }
    function get_password($username) {
        $validate = $this->db->query('SELECT password FROM users WHERE username = ?', array($username));
        return $validate->result();
    }
    function set_session($username) {
        $sessdata = array(
            'username' => $username,
            );
        $this->session->set_userdata($sessdata);
    }
}
?>

For some reason, it works, but just gives me failure no matter what...

#2
[eluser]dalebotha[/eluser]
Hi

I'm a relative new comer to CI and Php in general but my database queries have gone something like this:
Code:
function get_password($username) {
    
    $this->db->where('username', $username);

    $validate = $this->db->get('users', 1); // 'users' - table, 1 - limit

    if ( $validate->num_rows() == 1 )
    {
     return $validate->row_array();
    }
    else
    {
     return false;
    }

}

I'm too unfamiliar with CI check the rest of your code and whether your database interaction is syntactically correct but this is how I've been doing my db stuff.

I hope this helps. Smile

Dale

#3
[eluser]Bramme[/eluser]
Google 'codeigniter auth tutorial' Wink


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.