Hello,
Because nearly every site needs/wants a Member/User system and so did I for my sites, so I made a library for it. I’ll update when I need something, and post further versions ![]()
Download Link: Click Here! (yup, tabs messed outside my code (PHP Designer 2007))
So, currently my class can do:
- Register a user
- Log in (CI session based)
- Check if logged in (CI session based)
- Forgot password (email based, new password sent to email)
- Get data from database by username (be careful, encrypted passwords could be get!)
- Get age by month
I’ll explain every function and how to use them ![]()
How to install
Put the code posted above in
system\application\libraries\Userlib.php
Now open
system\application\config\config.php
And search for
$config[‘sess_use_database’]
Put that on TRUE.
Now open
system\application\config\autoload.php
Be sure that $autoload[‘libraries’] has all these in it:
‘database’, ‘session’, ‘Userlib’
And the SQL:
CREATE TABLE `ci_sessions` (
`session_id` varchar(40) NOT NULL default '0',
`ip_address` varchar(16) NOT NULL default '0',
`user_agent` varchar(50) NOT NULL,
`last_activity` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`session_id`)
);
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(70) NOT NULL,
`password` varchar(70) NOT NULL,
`email` varchar(255) NOT NULL,
`ip` varchar(15) NOT NULL,
`status` enum('0','1','2','3') NOT NULL,
PRIMARY KEY (`id`)
);
In the user table, these are standard fields. The userlib won’t work without. You can change the status though
I use:
0 = banned
1 = member
2 = moderator
3 = administrator
And now: The functions -> How to use!
Register
string $this->userlib->register( username, password, email [, status [, emailcheck [, ipcheck ] ] ] );
username: The username of the user
password: the password, this will be encrypted automaticly in: sha1( md5( password ) )
email: the email [for password retrieval and such]
status: (optional) the status to give the user, default = 1
emailcheck: (optional) Check if the email address has been used on a different account, default = TRUE
ipcheck: (optional) Check if the ip address has been used on a different account, default = TRUE
You echo this function, or send it save it as a variable. This way if there is an error message it will be put on your screen.
Examples of usage:
// Only required data
// This will check if my email and ip are used before
echo $this->userlib->register( "Xikeon", "mypass", "xikeon@gmail.com" );
// All data
// This will check my email, but not ip
// This will make my account status 3 (administrator on my settings)
echo $this->userlib->register( "Xikeon", "mypass", "xikeon@gmail.com", 3, TRUE, FALSE );
Login
boolean $this->userlib->login( username, password );
username: the username duh
password: the password duh
Example of usage:
if( $this->userlib->login( "Xikeon", "mypass" ) )
{
echo 'You are now logged in';
} else {
echo 'Your username and/or password is incorrect!';
}
Logged in
boolean $this->userlib->logged_in( );
Example of usage:
if( $this->userlib->logged_in( ) )
{
echo 'You are logged in!';
} else {
echo 'Please login to view this page!';
}
Forgot
boolean $this->userlib->forgot( email, length );
email: email of the user who forgot the password
length: length of new password which will be sent to the email
Example of use:
//create a password of 5 digits
$this->userlib->forgot( "xikeon@gmail.com", 5 );
Get Data
string $this->userlib->getData( username, what );
username: the username of who you want to grab the data from
what: row name of what you want to get out of the database from the username
Example of use:
// Grab the status
echo $this->userlib->getData( "Xikeon", "status" );
Get Age
integer $this->userlib->getAge( day, month, year );
day: day of birth
month: month of birth
year: year of birth
Example of use:
// Get my age, will return: 15
echo $this->userlib->getAge( "8", "2", "1992" );
So, that was it I guess. Report any problems please. If any idea’s, please PM them to me!
Greetings,
Mike.
