CodeIgniter Forums

Full Version: Need help on custom login lib
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Molchy[/eluser]
Hi,

I used for help SimpleLogin lib, removed what i don't need ... then i add-et some of my own requirements and tested it:
- Login OK
- Notifications OK
- Captcha OK
- Detecting user allready loged-in FALSE

MY lib:
Code:
class UserAuth{
    
    var $CI;
    var $login_status_msg = 'Prijava v cms sistem ...';
    
    function UserAuth(){
        $this->CI =& get_instance();
    }
    
    function Log_In($mail,$password,$captcha){
        
        //CLEANING CAPTCHA IN DB
           $expiration = time()-1800;
        $this->CI->db->query("DELETE FROM Captcha WHERE captcha_time < ".$expiration);        
    
        //CHECKING INPUTS
        if($mail == '' OR $password == '') {
            $this->login_status_msg = 'Vnesite e-naslov in geslo ...';
            return false;
        }
        //CHECKING IF ALLREADY LOGED IN - THIS IS NOT WORKING ?
        if($this->CI->session->userdata('U_Mail') == $mail) {
            $this->login_status_msg = 'Uporabnik z podanimi podatki je že prijavljen ...';
            return false;
        }
        
        //CHECKING CAPTCHA CODE
        $binds = array($captcha, $_SERVER['REMOTE_ADDR'], $expiration);
        $query = $this->CI->db->query('SELECT COUNT(*) AS count FROM Captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?', $binds);
        $row = $query->row();
    
        if ($row->count == 0){
            $this->login_status_msg = 'Vnesite pravilno zaščitno kodo ...';
            return false;
        }
        //CHECKING INPUT DATA WITH DB
        $this->CI->db->where('U_Mail', $mail);
        $query = $this->CI->db->getwhere('Users');

        if ($query->num_rows() > 0){
            $row = $query->row_array();
            
            //CHECKING PASSWORD
            if(md5($password) != $row['U_Password']) {
                $this->login_status_msg = 'Vnesite pravilno geslo ...';
                return false;
            }
            
            //DESTROYING SESSION
            $this->CI->session->sess_destroy();
            
            //CREATINS SESSION
            $this->CI->session->sess_create();
            
            //REMOVING PASSWORD FIELD FROM ARRAY
            unset($row['U_Password']);
            
            //SETTING SESSION
            $this->CI->session->set_userdata($row);
            
            //SETTING USER LOGED IN
            $this->CI->session->set_userdata(array('logged_in' => TRUE));                  
            return true;
        }else{
            $this->login_status_msg = 'Uporabnik z podanim e-naslovom ne obstaja ...';  
            return false;
        }    
    }
    
    function LogOut() {
        $this->CI->session->sess_destroy();
    }
    

}

How can i detect user allready loged -in wihtout database usage only session ?