Description
SimpleUser is meant to be a simple user creation and authentication module for use in any site. By default it stores a user's name, email address, password (encrypted of course), administrator status and timestamp of when the account was created. The email address is used as the login username. There is a class, DBTools, for creating the necessary database, tables and first administrator account. The main class, SimpleUser, is to be used by the main application for handling user accounts. SimpleUser is easily extended, (for example: to log user logins in a table) but already contains functions needed for all basic user management tasks.
This same information is available at http://www.bobk4.com/simpleuser.
Installation
Installation is an easy, 3 step process.
STEP 1
Edit the database configuration in SimpleUserConfig.php to match your settings. You can either specify a database that already exists, or give it the info for the one you would like it to create. Two tables are needed and will be created if they don't already exist: "{prefix}users" and "{prefix}user_creds".
STEP 2
Look at example.php for the simple code required to set up the database.
STEP 3
Look at example.php and documentation.html (this file) for the complete rundown of functions available for use in the 'User' class.
Files
Classes
DBTools.class.php
Used for creating the necessary database, tables and first administrator account. Only to be used for setup.
SimpleUser.class.php
The main class file. Handles the registration, editing, deleting, and authentication of user accounts, including the display of forms neccesary for those purposes.
Other
SimpleUserConfig.php
Required by both classes. Lets you configure your database connection details and several other options needed by the classes.
example.php
Examples of the most-used functions.
documentation.html
That's this file, silly goose.
Classes in this package
DBTools class
This class handles the creation of the database, necessary tables and first administrator account.
Variables
private $dbname;
Name of the DB (set in SimpleUserConfig.php)
private $dbuser;
Name of DB user (set in SimpleUserConfig.php)
private $dbpass;
DB Password (set in SimpleUserConfig.php)
private $dbhost;
Address of DB host (set in SimpleUserConfig.php)
private $prefix;
Prefix to prepend to table names (set in SimpleUserConfig.php)
private $dblink;
Connection link once DB connection is established
private $goodmsg;
Tracks setup process to be output for later debugging
Private Functions
make_tables()
Does the actual dirty work of creating the tables if they don't exist. Called from set_up_db().
throw_error($msg)
Will be called when an unrecoverable error occurs. Outputs the error to the screen and exits script execution.
Public Functions
set_up_db()
Creates the DB according to options in SimpleUserConfig.php, if it doesn't exist already. Calls make_tables().
check_db()
Checks all DB/table configuration. Also makes sure an admin account exists.
create_admin_account($email, $password[, $firstname, $lastname])
Creates the first admin account in the DB. $email and $password are required.
SimpleUser class
This class handles the registration, editing, deleting, and authentication of user accounts, including the display of forms neccesary for those purposes.
Variables
private $dbname;
Name of the DB (set in SimpleUserConfig.php)
private $dbuser;
Name of DB user (set in SimpleUserConfig.php)
private $dbpass;
DB Password (set in SimpleUserConfig.php)
private $dbhost;
Address of DB host (set in SimpleUserConfig.php)
private $prefix;
Prefix to prepend to table names (set in SimpleUserConfig.php)
private $dblink;
Connection link once DB connection is established
private $user_session;
The place inn $_SESSION where user session data can be stored (set in SimpleUserConfig.php)
private $min_pass_length;
The minimum required length for passwords (set in SimpleUserConfig.php)
private $error;
Stores errors that have occurred during script execution.
Private Functions
set_error($msg)
Will be called when an error occurs. Stores the error.
get_error($msg)
Returns all errors and clears error variable.
Public Functions
show_login_form($post_to)
Shows a basic form with fields to allow a user to log in. $post_to is the name of the script that will be handling the login.
show_registration_form($post_to)
Shows a basic form with fields to allow a user to create thier own account. $post_to is the name of the script that will be handling the registration.
show_profile_edit_form($post_to[, $userID])
Shows a basic form with fields to allow a user to update their profile (name, email, password, etc.) $post_to is the name of the script that will be handling the updating of the profile. $userID is the ID of a user to edit (if not the current user.) This only works if the current user is an administrator. If left blank, it will alllow the current user to change their own profile. Allows admins to give admin rights to other users.
process_login()
Authenticates the user log in and sets session variables for later use by scripts.
process_registration()
Processes the submitted form data for new user creation. Makes sure logins are unique via email_exists().
process_profile_edit()
Stores changes to a logged in user's profile. Makes sure logins are unique via email_exists().
delete_account($userID)
Will delete the account of the given userID.
email_exists($email)
Checks that the given email address exists as a login in the system. Returns true if yes, false otherwise.
get_userID()
Returns the userID for the current user.
user_info($userID)
Returns the name, email, admin status and timestamp the account was created for the given user, as an array.
is_logged_in()
Returns true if the current user is logged in, false otherwise.
is_admin()
Returns true if the current user is logged in as an administrator, false otherwise.
all_users()
Returns an array of all user info (not passwords) for every user. Only available to logged in administrators.
log_out()
Logs out the currently logged in user.
Copyright (c) 2011 Bob Kennedy | kennedy7@gmail.com