Cancellation Notice
Future development has been canceled. I recommend using a framework with built-in user authentication and authorization like Laravel, (*1)
uFlex 1.0.x
A simple all-in-one PHP user Authentication library.
This library is developed, maintained and tested in a PHP 5.3.x environment. The UnitTest also runs
on Travis-CI for PHP 5.4.x and PHP 5.5.x., (*2)
The single class file class.uFlex.php
code can be found on the Legacy Branch, (*3)
, (*4)
For more information:, (*5)
Upgrading from 0.9x versions...
Before updating you will need to run a SQL upgrade script. Make sure you backup your database before running
the upgrade script. Refer to the DB directory https://github.com/ptejada/uFlex/tree/master/db, (*6)
If not using Composer instead of including a PHP class you will include the autoload.php
script in
your application which will auto include the library classes as required., (*7)
If using composer then just include the vendor/autoload.php
in your application if it has not already
been included., (*8)
Overall version 1.0 takes a more object oriented approach and follows conventions more closely.
For more information check out the API Changes, (*9)
Getting Started
Including it in your project
If using Composer just add ptejada/uflex
as a dependency. Note the casing on uflex
,
all lowercase. Ex:, (*10)
{
"require": {
"ptejada/uflex": "1.*"
}
}
When using Composer use the vendor/autoload.php
script to include the library in your
project., (*11)
If not using Composer then clone this repository in your project. Use the autoload.php
script to include the library
in your project., (*12)
Configuring the User object
When the User
class is instantiated not much happens, the session is not initialized nor a DB connection is established.
This is to allow the class to be configured. Once configured the start()
method must be call in order for the user
authentication process to start. For Example:, (*13)
config->database->host = 'localhost';
$user->config->database->user = 'test';
$user->config->database->password = 'test';
$user->config->database->name = 'uflex_test'; //Database name
// OR if in your project you already have a PDO connection
// $user->config->database->pdo = $existingPDO;
/*
* You can update any customizable property of the class before starting the object
* construction process
*/
//Start object construction
$user->start();
?>
It is preferable that a configuration file like the one above is created per project. This way you can use the configuration
file to provide a pre-configured User
instance to any PHP script in your project., (*14)
Alternatively you could create your own class which configures and start the the User
object for you. Ex:, (*15)
config->database->host = 'localhost';
$this->config->database->user = 'test';
$this->config->database->password = 'test';
$this->config->database->name = 'uflex_test'; //Database name
// Start object construction
$this->start();
}
}
?>
Below is an excerpt from the PHP class file which lists the customizable config
properties you could change prior to calling
start()
on a User
instance. Note: the config
property is a Collection
instance:, (*16)
'cookieTime' => '30',
'cookieName' => 'auto',
'cookiePath' => '/',
'cookieHost' => false,
'userTableName' => 'users',
'userSession' => 'userData',
'userDefaultData' => array(
'Username' => 'Guess',
'ID' => 0,
'Password' => 0,
),
'database' => array(
'host' => 'localhost',
'name' => '',
'user' => '',
'password' => '',
'dsn' => '',
'pdo' => '',
)
Understanding Collections
A Collection
is an object representation of an array. Collection
s have many uses throughout this project and are
easy to use. What a Collection
does for us is handle the errors for undefined indexes and streamline our code., (*17)
Consider this example working with plain arrays:, (*18)
'pablo',
);
if (isset($data['quote']) && $data['quote'])
{
echo $data['name'] . "'s quote is: " . $data['quote'];
}
else
{
echo $data['name'] . " has no quote";
}
?>
Here is the same code using a Collection
:, (*19)
'pablo',
));
if ($data->quote)
{
echo "{$data->name}'s quote is: $data->quote";
}
else
{
echo "{$data->name} has no quote";
}
?>
For more information check the API Documentation for the Collection
class., (*20)
Using the Session
The User
object provides easy management of the PHP session through its session
property which is an instance of
the Session
class. By default the User
class manages the userData
namespace in PHP super global $_SESSION but this
is configurable by setting config->userSession
before the User
object is started. This is very powerful since it lets
the User
class use the PHP session without interfering with other software the their session usage., (*21)
The Session
class is just an extended Collection
so it works like any other collection. The only difference is a few
additional methods and the fact that it is a linked collection meaning that any changes made in the object will be
reflected on $_SESSION
and thus automatically saved on the PHP session., (*22)
Consider the following code and its output to give you a better idea of how everything works together:, (*23)
config->userSession = 'myUser';
$user->start();
// Shows session right after the User object has been started
print_r($_SESSION);
// Stores something in the session
$user->session->myThing = 'my thing goes here';
// Shows the session with the content of the myThing property
print_r($_SESSION);
// Stores something in the PHP session outside of the namespace scope managed by the User class
$_SESSION['other'] = 'other information stored here';
print_r($_SESSION);
// Only destroys the session namespace managed by the User Class
$user->session->destroy();
print_r($_SESSION);
?>
Here is the output of the previous code:, (*24)
Array
(
[myUser] => Array
(
[data] => Array
(
[Username] => Guess
[ID] => 0
[Password] => 0
)
)
)
Array
(
[myUser] => Array
(
[data] => Array
(
[Username] => Guess
[ID] => 0
[Password] => 0
)
[myThing] => my thing goes here
)
)
Array
(
[myUser] => Array
(
[data] => Array
(
[Username] => Guess
[ID] => 0
[Password] => 0
)
[myThing] => my thing goes here
)
[other] => other information stored here
)
Array
(
[other] => other information stored here
)
The Session
class can be use for other aspects of your application as well. For example to manage the entire PHP
session you could do so by instantiating the Session class without arguments: new ptejada\uFlex\Session()
, (*25)
For more information on the Session
class refer to the API Documentation, (*26)
Extending the User class
In PHP you area able extend classes just like in any object oriented programming language. Therefore you could extend
the User
class functionality by adding your methods or modifications without having to modify the class file itself. You
just have create a new PHP class that extends the User
class:, (*27)
<php
class User extends ptejada\uFlex\User{
/*
* Add your default properties values
* such as database connection credentials
* user default information
* Or cookie preferences
*/
/*
* Create your own methods
*/
function updateAvatar(){}
function linkOpeniD(){}
}
?>