Example 28-1. Typical usage example for PEAR::Auth
require_once "Auth.php";
function loginFunction()
{
/*
* Change the HTML output so that it fits to your
* application.
*/
echo "<form method=\"post\" action=\"test.php\">";
echo "<input type=\"text\" name=\"username\">";
echo "<input type=\"password\" name=\"password\">";
echo "<input type=\"submit\">";
echo "</form>";
}
$dsn = "mysql://user:password@localhost/database";
$a = new Auth("DB", $dsn, "loginFunction");
$a->start();
if ($a->checkAuth()) {
/*
* The output of your site goes here.
*/
} |
This few lines of code instantiate the authentication system.
The first line in the above script includes the file from your
PEAR directory. It contains all the necessary code to run
PEAR::Auth. Next, we define a function
to display the login form which the visitor of your page has to
use to enter his login data. You can change all the HTML
formatting in this function.
Since we want to use a database to verify the login data, we now
create the variable $dsn, it contains a valid
DSN string that will be used to connect to the database via
PEAR::DB. For the default
database table schema or to use a different storage container,
please see below for more information.
After that we create the authentication object. The first
parameter defines the name of the storage container. Because we
want to use a database driven storage container, we pass "DB"
here. The second parameter is the connection parameter for the
container driver. We use the previously defined DSN string. The
third parameter is the name of our function that we defined at the
beginning of the script. It prints the login form.
Now our authentication object is initialized and we need to check
if the user is logged in. This is done via the method
checkAuth(). If it returns TRUE, we can pass the
content of our page to the user.