Part of the EllisLab Network
   
1 of 4
1
Userlib - User Library
Posted: 04 July 2007 11:43 AM   [ Ignore ]  
Summer Student
Total Posts:  12
Joined  12-16-2006

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 smile

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 smile

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_idvarchar(40NOT NULL default '0',
  `
ip_addressvarchar(16NOT NULL default '0',
  `
user_agentvarchar(50NOT NULL,
  `
last_activityint(10unsigned NOT NULL default '0',
  
PRIMARY KEY  (`session_id`)
);

CREATE TABLE `users` (
  `
idint(11NOT NULL auto_increment,
  `
usernamevarchar(70NOT NULL,
  `
passwordvarchar(70NOT NULL,
  `
emailvarchar(255NOT NULL,
  `
ipvarchar(15NOT NULL,
  `
statusenum('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 wink 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"3TRUEFALSE ); 

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"); 

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.

Profile
 
 
Posted: 04 July 2007 12:27 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  52
Joined  06-12-2006

Good work! I’m going to test it. Thanks for sharing.

Profile
 
 
Posted: 04 July 2007 02:15 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  12
Joined  12-16-2006

Thanks,
please tell me if you find any problems or notice anything wink

Profile
 
 
Posted: 04 July 2007 02:21 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  2
Joined  07-01-2007

I am very new to CI and I was just starting to look for an example of user control code. I will also start trying it and see how I get on.

 Signature 

Many Thanks
John Messingham
http://www.jmds.co.uk

Profile
 
 
Posted: 04 July 2007 02:26 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  12
Joined  12-16-2006

Hey,
thanks for trying my class. If you need any help or don’t understand (parts of) my library, just post here PM me. I might be able to help you smile

Good luck!

Profile
 
 
Posted: 04 July 2007 02:31 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  3
Joined  06-30-2007

Looks great, I will use it smile

Profile
 
 
Posted: 04 July 2007 02:44 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  12
Joined  12-16-2006

Thanks Sander.

I’ve updated the link, now to my own pastebin. I also fixed some typo’s in the comments, because I changed/updated functions and didn’t change the comments. Not really a needed update, just comment fixes. wink

Profile
 
 
Posted: 06 July 2007 02:08 AM   [ Ignore ]   [ # 7 ]  
Summer Student
Avatar
Total Posts:  2
Joined  01-13-2007

Btw. handling the “Forgot Password” function like you do is an open invitation for troublemakers… store the new password in an additional field and don’t overwrite the old one until the user logs himself in with the new password - otherwise, somebody could abuse your “forget password” function to render all logins of a site useless until their resprective owners have read the mail (just imagine a simple script that scans your site for usernames and then invokes your forget password page with all found usernames)

 Signature 

Btw. my Username is my real name… It has absolutly nothing to do with religion or stuff like that tongue wink ((In fact, I’m not religious at all - I just wanted to mention this because some people on other forums already thought I choose my Nickname based on religious believes))

Profile
 
 
Posted: 06 July 2007 04:01 AM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  12
Joined  12-16-2006
Christian Land - 06 July 2007 06:08 AM

Btw. handling the “Forgot Password” function like you do is an open invitation for troublemakers… store the new password in an additional field and don’t overwrite the old one until the user logs himself in with the new password - otherwise, somebody could abuse your “forget password” function to render all logins of a site useless until their resprective owners have read the mail (just imagine a simple script that scans your site for usernames and then invokes your forget password page with all found usernames)

Yes I know, I was a bit lazy on that function.. Hehe.

I will make it better in the next version. smile

Profile
 
 
Posted: 06 July 2007 02:00 PM   [ Ignore ]   [ # 9 ]  
Grad Student
Rank
Total Posts:  58
Joined  10-24-2006

Since you are getting peoples emails, why not make that their login authentication variable, rather than their username? Since, hopefully, people won’t be making email addresses visible on the site, this would also cut down on people trying to brute force password using screenname/password

Profile
 
 
Posted: 06 July 2007 06:35 PM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  12
Joined  12-16-2006
jbowman - 06 July 2007 06:00 PM

Since you are getting peoples emails, why not make that their login authentication variable, rather than their username? Since, hopefully, people won’t be making email addresses visible on the site, this would also cut down on people trying to brute force password using screenname/password

I’m sorry? I don’t really understand what you mean? Why would email as login auth be more secure?

Profile
 
 
   
1 of 4
1