-
Notifications
You must be signed in to change notification settings - Fork 4
Users Library
After pulling my hair out in frustration with the state of some of the User [strike]Authorization[/strike] Authentication libraries available for Code Igniter, I decided to write my own.
Place the Users.php file inside of your system/application/library/ folder.
You also need the Db_session library installed.
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(25) NOT NULL default '',
`email` varchar(100) NOT NULL default '',
`fname` varchar(25) NOT NULL default '',
`lname` varchar(25) NOT NULL default '',
`addr` varchar(255) NOT NULL default '',
`city` varchar(25) NOT NULL default '',
`state` varchar(25) NOT NULL default '',
`country` varchar(25) NOT NULL default '',
`zip` int(11) NOT NULL default '0',
`timezone` int(11) NOT NULL default '0',
`isadmin` tinyint(1) NOT NULL default '0',
`password` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
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;
<?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', $data, true);
$output .= $this->load->view('user/user', $data, true);
$output .= $this->load->view('common/footer', $data, true);
$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', $data, true);
$output .= $this->load->view('user/register', $data, true);
$output .= $this->load->view('common/footer', $data, true);
$this->output->set_output($output);
}
function registered()
{
$data = array(
'header_img'=>'header_logo.gif',
'show_nav'=>false
);
$output = $this->load->view('common/header', $data, true);
$output .= $this->load->view('user/registered', $data, true);
$output .= $this->load->view('common/footer', $data, true);
$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', $data, true);
$output .= $this->load->view('user/main', $data, true);
$output .= $this->load->view('common/footer', $data, true);
$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', $data, true);
$output .= $this->load->view('user/logout', $data, true);
$output .= $this->load->view('common/footer', $data, true);
$this->output->set_output($output);
}
}
?>