Simple authentication tutorial
The setup
So, now I'm going to show you how the SimpleAuth
class works.
The idea behind it is to provide a simple authentication process when you
don't need any bells and whistles! The SimpleAuth
class can hold the following user information:
- ID
- Username
- Password
- Real name
- Level - that can be useful if you want users to have different access levels.
- Appname - so that you can use the same database and table for multiple sites/applications/logins
This information is stored in a SQLite or
MySQL database. You can either provide an existing
DB object or let SimpleAuth create one for you.
To keep down the number of SQL executions you have to invoke the creation of the table it self manually, and while you'r at it you might as well add your self as an user. This is typically how you would do to create the table and add your self as a user:
- <?php
- require_once 'PLib.php';
- PLib::Import('Security.SimpleAuth');
- //! If you have an existing DB-object that could be passed as the first
- //! argument instead of the connection string here.
- $auth = new SimpleAuth('sqlite:///path/to/db.sqlite', 'myapp');
- $auth->SetupTable();
- //! Username, Password, Real name, Email, Level
- $auth->AddUser('admin', 'password', 'Pontus Östlund', 'spam@poppa.se', 1);
- ?>
When this is done you would probably want to remove the calls to
SetupTable and AddUser.
One thing worth noticing is that the password is run through
sha1() and salted with a standard salt that's defined
in the class. You can replace the standard salt with your own by calling the
static mehod SetSalt prior to calling
AddUser.
- $auth = new SimpleAuth('sqlite:///path/to/db.sqlite', 'myapp');
- SimpleAuth::SetSalt('my452Very123MysterI5322OUs892Sal65t');
- //! And the rest of the code
To verify that your newly created user is created you can call
the method Inspect which will print out a table
of all the users
- $auth = new SimpleAuth('sqlite:///path/to/db.sqlite', 'myapp');
- $auth->Inspect();
So that's the basic setup.
Now lets do some authentication »