Welcome Guest, Not a member yet? Register   Sign In
Problem with user authentication
#1

[eluser]elof[/eluser]
First of all, hello! I'm a new CodeIgniter developer and this is my first post to the forums.

I've been making a community application, which is incredibly basic at the moment, but I've run into a problem when making a user authentication system, i.e. a login system. I know there are several authentication libraries like DX Auth for CI, but I'd like to try to make one myself before diving into separate libraries in order to gain some experience.

The login works like this:
- User enters username and password in a form, which is present on every page of the site (i.e. there is no static login page - there's a login bar on each page)
- Form sends data to a function named login
- Username and password are matched through a database
- If password is correct, the login function sets 2 session variables: logged_in => TRUE and username => submitted username
- User is redirected to the page they were previously on, using a hidden form field containing the previous url

Now, the problem is this: when the user logs in for the first time, nothing happens - they're redirected to the previous page as if it worked, but the session data is lost. If they log in again, it works fine and the user is successfully logged in. It's only on the first login after a cold boot (i.e. user visits the page for the first time after starting their browser) that it won't work.

I'm guessing that the session data is lost along the way, but I can't find why it's being lost... I've tried replacing the redirect (which is happening through the URL helper) with a simple "Go back." link on success, and even then the session data is lost.

I won't post the entire controller or the model, there's a bunch of stuff there unrelated to the problem, but here are the relevant functions:
Code:
function login() {
        $this->load->library('session');
        
        // Selects username from database and sees if submitted password matches stored password
        if(!$this->_password_check($this->input->post('username'), $this->input->post('password'))) {
            $error ="<p>Username or password incorrect.</p>";
            $this->load->view('head');
            $this->load->view('nav');
            $this->load->view('info');
            $this->load->view('login_view', array('error' => $error));
            $this->load->view('footer');
        } else {
            $this->session->set_userdata(array(
              'logged_in' => TRUE,
              'username'  => $this->input->post('username')
            ));
            redirect($this->input->post('current_url'));
        }
}

This function checks if the username and password match through a database
Code:
function _password_check($username, $password) {
        if(empty($username) || empty($password)) {
            return false;
        }
        $this->load->database();
        $this->load->library('encrypt');
        $query = $this->db->get_where('users', array('username' => $username), 1, 0);
        if($query->num_rows()> 0) {
            $result = $query->row_array();
            if($result['password_sha1'] == $this->encrypt->sha1($password)) {
                return true;
            }
        }
        return false;
}

I hope I'm not missing anything too obvious... any help will be much appreciated. Thanks!


Messages In This Thread
Problem with user authentication - by El Forum - 09-16-2009, 07:52 AM
Problem with user authentication - by El Forum - 09-16-2009, 09:13 AM
Problem with user authentication - by El Forum - 09-16-2009, 09:29 AM
Problem with user authentication - by El Forum - 09-16-2009, 10:08 AM
Problem with user authentication - by El Forum - 09-16-2009, 10:16 AM
Problem with user authentication - by El Forum - 09-16-2009, 12:11 PM
Problem with user authentication - by El Forum - 10-20-2009, 02:22 PM
Problem with user authentication - by El Forum - 03-26-2010, 11:11 AM



Theme © iAndrew 2016 - Forum software by © MyBB