Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by walrus_lt )

Users Library

After pulling my hair out in frustration with the state of some of the User Authorization Authentication libraries available for Code Igniter, I decided to write my own.

Installation


File:Users 0.5.zip

Place the Users.php file inside of your system/application/library/ folder.

You also need the DB Session library installed.

Database

CREATE TABLE `users` (
  `
idint(11NOT NULL auto_increment,
  `
usernamevarchar(25NOT NULL default '',
  `
emailvarchar(100NOT NULL default '',
  `
fnamevarchar(25NOT NULL default '',
  `
lnamevarchar(25NOT NULL default '',
  `
addrvarchar(255NOT NULL default '',
  `
cityvarchar(25NOT NULL default '',
  `
statevarchar(25NOT NULL default '',
  `
countryvarchar(25NOT NULL default '',
  `
zipint(11NOT NULL default '0',
  `
timezoneint(11NOT NULL default '0',
  `
isadmintinyint(1NOT NULL default '0',
  `
passwordvarchar(255NOT NULL default '',
  
PRIMARY KEY  (`id`)
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=

Usage

As with all Code Igniter libraries, you must load it before you use it.

$this->load->library('users'); 

To check if a user is logged in:

//Check if the user is logged in
if(!$this->users->isLoggedIn())
{
     redirect
('user/login');

To login a user:

if( !$this->users->login($this->input->post('username'),$this->input->post('password')) )
{
     $error 
'error logging in';

To register a user:

if(!$this->users->register($username,$password,$email))
{
    $error 
$this->users->last_error;
}
else
{
    redirect
('user/registered');

To log a user out:

$this->users->logout(); 

To grab information about a user:

echo $this->users->getInfo($this->users->user,'fname'); 

Note: the second parameter is a reference to the database field containing the data. You may not request the password field.

To update a user’s information:

$data 'Billy';
$this->users->updateInfo($username,'fname',$data); 

To validate a user’s password:

$userdata unserialize($this->ci->encrypt->decode($this->ci->db_session->userdata('user')));
            
if( 
$this->_validatePass($userdata['username'],$userdata['password']) )
{
     $this
->user $userdata['username'];
     return 
true;

Note: This function was built to only be used by internal functions, however there are some cases where it would be useful.

To recover a user’s password:

$stored_password $this->users->recoverPassword($email);
//send an email 

Note: This function needs work. Ideally a new password would be put in a temporary field in the database.

To get the current logged in user:

echo $this->users->user

 

 

Sample Controller

<?php
class User extends Controller {

    
function User()
    
{
        parent
::Controller();
        
$this->load->library('users');
    
}
    
    
function index()
    
{
        $this
->main();
    
}
    
    
function login()
    
{
        
if($this->input->post('username')!=''&&$this->input->post('password')!='')
        
{
            
if( !$this->users->login($this->input->post('username'),$this->input->post('password')) )
            
{
                $error 
1;
            
}
        }
        
        
//Check if the user is already logged in
        
if($this->users->isLoggedIn())
        
{
            redirect
('user/main');
        
}
        
        $data 
= array(
            
'header_img'=>'header_logo.gif',
            
'show_nav'=>false,
            
'error'=>$error
            
);
        
$output $this->load->view('common/header'$datatrue);
        
$output .= $this->load->view('user/user'$datatrue);
        
$output .= $this->load->view('common/footer'$datatrue);
        
        
$this->output->set_output($output);
    
}
    
    
function register()
    
{
        
if($this->input->post('userregister'))
        
{
            
if($this->input->post('username')!='')
            
{
                $error 
"Please enter a username.";
            
}
            
            
if($this->input->post('email')!='')
            
{
                $error 
"Please enter your email.";
            
}
            
            
if($this->input->post('password')!=$this->input->post('password2'))
            
{
                $error 
"Passwords do not match.";
            
}
            
            
            $username 
trim($this->input->post('username'));
            
$email trim($this->input->post('email'));
            
$password trim($this->input->post('password'));
            
            if(!
$this->users->register($username,$password,$email))
            
{
                $error 
$this->users->last_error;
            
}
            
else
            
{
                redirect
('user/registered');
            
}
        }
        
        
//Check if the user is already logged in
        
if($this->users->isLoggedIn())
        
{
            redirect
('user''location');
        
}
        
        $data 
= array(
            
'header_img'=>'header_logo.gif',
            
'show_nav'=>false,
            
'error'=>$error
            
);
        
$output $this->load->view('common/header'$datatrue);
        
$output .= $this->load->view('user/register'$datatrue);
        
$output .= $this->load->view('common/footer'$datatrue);
        
        
$this->output->set_output($output);
    
}
    
    
function registered()
    
{
        $data 
= array(
            
'header_img'=>'header_logo.gif',
            
'show_nav'=>false
            
);
        
$output $this->load->view('common/header'$datatrue);
        
$output .= $this->load->view('user/registered'$datatrue);
        
$output .= $this->load->view('common/footer'$datatrue);
        
        
$this->output->set_output($output);
    
}
    
    
function main()
    
{
        
//Check if the user is already logged in
        
if(!$this->users->isLoggedIn())
        
{
            redirect
('user/login');
        
}
        
        $data 
= array(
            
'header_img'=>'header_logo.gif',
            
'show_nav'=>false,
            
'first_name'=>$this->users->getInfo($this->users->user,'fname'),
            
'last_name'=>$this->users->getInfo($this->users->user,'lname'),
            
'user_email'=>$this->users->getInfo($this->users->user,'email'),
            
'last_name'=>$this->users->getInfo($this->users->user,'lname'),
            
'user_address'=>$this->users->getInfo($this->users->user,'addr'),
            
'user_city'=>$this->users->getInfo($this->users->user,'city'),
            
'user_country'=>$this->users->getInfo($this->users->user,'country'),
            
'user_zip'=>$this->users->getInfo($this->users->user,'zip'),
            
'username'=>$this->users->user
            
);
        
$output $this->load->view('common/header'$datatrue);
        
$output .= $this->load->view('user/main'$datatrue);
        
$output .= $this->load->view('common/footer'$datatrue);
        
        
$this->output->set_output($output);
    
}
    
    
function logout()
    
{
        $this
->users->logout();
        
        
$data = array(
            
'header_img'=>'header_logo.gif',
            
'show_nav'=>false
            
);
        
$output $this->load->view('common/header'$datatrue);
        
$output .= $this->load->view('user/logout'$datatrue);
        
$output .= $this->load->view('common/footer'$datatrue);
        
        
$this->output->set_output($output);
    
}
}
?> 

Category:Libraries -> Authentication