CodeIgniter Forums
Userlib - User Library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Userlib - User Library (/showthread.php?tid=1911)



Userlib - User Library - El Forum - 07-04-2007

[eluser]Xikeon[/eluser]
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:
Code:
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 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:
Code:
// Only required data
// This will check if my email and ip are used before
echo $this->userlib->register( "Xikeon", "mypass", "[email protected]" );

// 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", "[email protected]", 3, TRUE, FALSE );

Login
boolean $this->userlib->login( username, password );

username: the username duh
password: the password duh

Example of usage:
Code:
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:
Code:
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:
Code:
//create a password of 5 digits
$this->userlib->forgot( "[email protected]", 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:
Code:
// 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:
Code:
// 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.


Userlib - User Library - El Forum - 07-04-2007

[eluser]ztinger[/eluser]
Good work! I'm going to test it. Thanks for sharing.


Userlib - User Library - El Forum - 07-04-2007

[eluser]Xikeon[/eluser]
Thanks,
please tell me if you find any problems or notice anything Wink


Userlib - User Library - El Forum - 07-04-2007

[eluser]Unknown[/eluser]
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.


Userlib - User Library - El Forum - 07-04-2007

[eluser]Xikeon[/eluser]
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!


Userlib - User Library - El Forum - 07-04-2007

[eluser]Unknown[/eluser]
Looks great, I will use it Smile


Userlib - User Library - El Forum - 07-04-2007

[eluser]Xikeon[/eluser]
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


Userlib - User Library - El Forum - 07-06-2007

[eluser]Christian Land[/eluser]
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)


Userlib - User Library - El Forum - 07-06-2007

[eluser]Xikeon[/eluser]
[quote author="Christian Land" date="1183720119"]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)[/quote]

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

I will make it better in the next version. Smile


Userlib - User Library - El Forum - 07-06-2007

[eluser]jbowman[/eluser]
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