-
Notifications
You must be signed in to change notification settings - Fork 0
Users Library
Category:Libraries:Authorization
After pulling my hair out in frustration with the state of some of the User Authorization libraries available for Code Igniter, I decided to write my own.
Download: 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.
Use this SQL in your database.
[code]
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 ;
[/code]
[b]Usage[/b] As with all Code Igniter libraries, you must load it before you use it. [code]$this->load->library('users');[/code]
To check if a user is logged in: [code] //Check if the user is logged in if(!$this->users->isLoggedIn()) { redirect('user/login'); } [/code]
To login a user: [code] if( !$this->users->login($this->input->post('username'),$this->input->post('password')) ) { $error = 'error logging in'; } [/code]
To register a user: [code] if(!$this->users->register($username,$password,$email)) { $error = $this->users->last_error; } else { redirect('user/registered'); } [/code]
To log a user out: [code] $this->users->logout(); [/code]
To grab information about a user: [code] echo $this->users->getInfo($this->users->user,'fname'); [/code] [b]Note:[/b] 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: [code] $data = 'Billy'; $this->users->updateInfo($username,'fname',$data); [/code]
To validate a user's password: [code] $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; } [/code] [b]Note:[/b] 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: [code] $stored_password = $this->users->recoverPassword($email); //send an email [/code] [b]Note:[/b] 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: [code] echo $this->users->user; [/code]
Sample controller: [code] <?php class User extends Controller {
function User()
{
parent::Controller();
$this->load->library('users');
}
function index()
{
redirect('user/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);
}
} ?> [/code]