Welcome Guest, Not a member yet? Register   Sign In
(SOLVED)Need Login Help using Erkanaauth
#9

[eluser]zimco[/eluser]
POST CONTINUED....

Here's the code for my login view, content-sentry.php

Code:
<div id="content">

<div id="login">
<h1>Login</h1>

<div id="login_error">
&lt;?php echo $this->validation->error_string; ?&gt;
</div>

&lt;?php $attributes = array('id' => 'login'); ?&gt;

&lt;?php echo form_open('admin/sentry', $attributes); ?&gt;

<p>
&lt;?php
  echo form_label('Username: ', 'username');
  
  $username = array(
              'name'    => 'username',
              'maxlength'   => '10',
              'size'        => '25',
            );
  echo form_input($username);
  ?&gt;
</p>

<p>
&lt;?php
  echo form_label(' Password: ', 'username');
  $password = array(
              'name'    => 'password',
              'maxlength'   => '10',
              'size'        => '25',
            );
  echo form_password($password);
  ?&gt;
</p>
  
<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;
  
</div>

</div>

Here's my code for Erkanaauth.php

Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Erkanaauth {

    var $CI;
    
    function Erkanaauth() {
        $this->CI =& get_instance();
        log_message('debug', 'Authorization class initialized.');
        
        $this->CI->load->database();
        $this->CI->load->library('session');
        $this->CI->load->library('encrypt');
        $this->CI->load->helper('erkanaauth_helper');
    }
    
    /**
     * Attempt to login using the given condition
     *
     * Accepts an associative array as input, containing login condition
     * Example: $this->auth->try_login(array('email'=>$email, 'password'=>dohash($password)))
     *
     * @access    public
     * @param    array    login conditions
     * @return    boolean
     */    
    function try_login($condition = array()) {
    
    $password = $condition['password'];
    //Salt the password see http://www.haughin.com/2008/02/17/handling-passwords-in-codeigniter/
    $condition['password'] = $this->_prep_password($password);
    
    $this->CI->db->select('id');
        $query = $this->CI->db->get_where('users', $condition, 1, 0);
        if ($query->num_rows != 1) {
            return FALSE;
        } else {
            $row = $query->row();
            $this->CI->session->set_userdata(array('user_id'=>$row->id));
            return TRUE;
        }
    }
    
    
    /**
     * Attempt to login using session stored information
     *
     * Example: $this->auth->try_session_login()
     *
     * @access    public
     * @return    boolean
     */
    function try_session_login() {
        if ($this->CI->session->userdata('user_id')) {
            $query = $this->CI->db->query('SELECT COUNT(*) AS total FROM users WHERE id = ' . $this->CI->session->userdata('user_id'));
            $row = $query->row();
            if ($row->total != 1) {
                // Bad session - kill it
                $this->logout();
                return FALSE;
            } else {
                return TRUE;
            }
        } else {
            return FALSE;
        }
    }
    
    
    /**
     * Logs a user out
     *
     * Example: $this->erkanaauth->logout()
     *
     * @access    public
     * @return    void
     */
     function logout() {
        $this->CI->session->set_userdata(array('user_id'=>FALSE));
    }
    
    
    /**
     * Returns a field from the user's table for the logged in user
     *
     * Example: $this->erkanaauth->getField('username')
     *
     * @access    public
     * @param    string    field to return
     * @return    string
     */
    function getField($field = '') {
        $this->CI->db->select($field);
        $query = $this->CI->db->get_where('users', array('id'=>$this->CI->session->userdata('user_id')), 1, 0);
      if ($query->num_rows() == 1) {
            $row = $query->row();
            return $row->$field;
        }
    }
    
    /**
     * Returns the user's role
     *
     * Example: $this->erkanaauth->getRole()
     *
     * @access    public
     * @return    string
     */
    function getRole() {
        $this->CI->db->select('roles.name');
        $this->CI->db->JOIN('roles', 'users.role_id = roles.id');
        $query = $this->CI->db->get_where('users', array('users.id'=>$this->CI->session->userdata('user_id')), 1, 0);
        if ($query->num_rows() == 1) {
            $row = $query->row();
            return $row->name;
        }
    }
    

/*======================================================================*\
    Function:  _prep_password($password)
    Purpose:   Salt the password with an encryption key
    Input:       password string
    Output:       password string plus encryption key
\*======================================================================*/    
function _prep_password($password)
{
     $mykey = sha1($password.$this->CI->config->item('encryption_key'));
     return $mykey;
}
    
/*======================================================================*\
                                  END
\*======================================================================*/    
}

?&gt;


Messages In This Thread
(SOLVED)Need Login Help using Erkanaauth - by El Forum - 06-27-2008, 07:29 AM
(SOLVED)Need Login Help using Erkanaauth - by El Forum - 06-27-2008, 12:25 PM
(SOLVED)Need Login Help using Erkanaauth - by El Forum - 06-27-2008, 12:51 PM
(SOLVED)Need Login Help using Erkanaauth - by El Forum - 06-27-2008, 02:03 PM
(SOLVED)Need Login Help using Erkanaauth - by El Forum - 06-27-2008, 02:49 PM
(SOLVED)Need Login Help using Erkanaauth - by El Forum - 06-28-2008, 06:19 AM
(SOLVED)Need Login Help using Erkanaauth - by El Forum - 06-28-2008, 09:35 AM
(SOLVED)Need Login Help using Erkanaauth - by El Forum - 06-29-2008, 03:38 PM
(SOLVED)Need Login Help using Erkanaauth - by El Forum - 06-29-2008, 04:17 PM



Theme © iAndrew 2016 - Forum software by © MyBB