====== PHP Users Class ======
I got frustrated trying to find a class or library to authenticate user logins in PHP. The ones I found were either too simplistic, or required me to "join" some project just to have access to purportedly open source software.
So, I decided to dust off the neurons and see if I could build one. I decided to make it as flexible as possible, with only the very basics, but able to be enhanced via data calls. I also decided to make the data access independent of the class itself so data access classes could be written for tasks other than MySQL using the mysqli library.
Because of this, usersDataSource is an abstract class which can not be instantiated. Instead, you must extend the class, defining all of the abstract methods in the abstract. We've done this with the UsersDataSourceMySQLi class.
By itself, the users class (with a data access class usersDataSource like the included UsersDataSourceMySQLi class) handles basic login/logout/editing functions.
The class(es) were, however, designed for extensibility and customization in mind. Some design considerations were made with this in mind.
NOTE: This code uses the ternary and null coelescing shortcuts ?: and ??. I THINK these were introduced in PHP 5.3, but not sure. This code will not work on versions which do not have these shortcuts. See https://www.php.net/manual/en/language.operators.comparison.php
You can get a copy of this from our subversion repository
svn co http://svn.dailydata.net/svn/php_users/tags/stable php_users
My working copy is at
http://svn.dailydata.net/svn/php_users/trunk
but I recommend NOT using that as I use trunk as my personal playground and will commit broken code to it regularly
An extension of this basic class which adds boolean permissions is the [[software:dailydata:libraries:php_user_permissions|UsersPermissions]] class. It is part of the same library.
==== Basic System ====
With no modification, the system will store username, password and two booleans, isAdmin and enabled. The default table is created as
create or replace table _users (
_user_id int unsigned not null auto_increment,
login varchar(64), /* the login name */
password varchar(128), /* encrypted form of the password */
isAdmin boolean DEFAULT 1, /* if true, user has full admin rights */
enabled boolean DEFAULT 1, /* if false, user can not log in */
primary key (_user_id)
);
* login is filtered to alpha-numerics and the underscore character
* password is stored as a hash using PHP's password_hash
* users with isAdmin set will be able to add/edit other users
* users with enabled set to false (0) will not be able to log in
NOTE: the usersDataSourceMySQLi class has a public function, buildTable, which will build the table, so installation involves simply calling that function if you are using that data source class.
IMPORTANT: to allow the Users class to work with a wide variety of data types, it does no data access itself. It requires a data access class.
Basic use in a script involves instantiating a data access class object, then instantiating a Users class object.
Example:
'test', 'password' => 'test', 'database' => 'test' )
);
//$connection->buildTable( 'admin', 'admin' ); die;
// ensure we always have a (possibly invalid) instance of user
if ( ! isset( $_SESSION['user'] ) ) {
$_SESSION['user'] = new Users( );
}
?>
This example is using the UsersDataSourceMySQLi definition of data access (included)
If you run it the first time with $connection->buildTable( 'admin', 'admin' ); die;
uncommented, it will build the table. Comment that line out on the next run and you will be presented with a login screen.
Class function HTML() displays various things to allow login, then quits displaying anything. Setting $_REQUEST['logout'] = 1 before calling HTML() will initiate a log out which will destroy the session variable
==== Full Functionality ====
Calling the class method admin and displaying the repeatedly will generate output allowing the user to change their username, password and any other fields which do not have the 'restrict' attribute set to true
If the user has admin rights set, it will also display a list of logins and allow you to select one to edit or, add a new user. Editing someone else shows all fields, whether or not the 'restrict' attribute is set to true.
==== CSS ====
I tried to not put any HTML layout into the code, relying instead on CSS (Thanks, Randell). Everything is supposed to have a class and be wrapped in a