Auth::Auth()

Auth::Auth() – constructor

Synopsis

Auth::Auth ( mixed $storageDriver , mixed $options = "" , string $loginFunction = "" , boolean $showLogin = true )

Description

Constructor for the authentication system.

The constructor will ensure that the PHP session management is started by calling session_start(). This is done as Auth requires a session to be active to operate correctly.

Parameter

string $storageDriver

Name of the storage driver that should be used, alternatively you can pass a custom Auth_Container object.

mixed $options

Array containing the options for both Auth itself and the storage driver. See Auth Options for global options and each storage driver's documentation for specific options.

string $loginFunction

The name of a user-defined function that prints the login screen. This function is passed three parameters when called $username, $status, &$auth. These are in order the previously attempted username, the status code that caused the previous auth attempt to fail and a reference to the Auth object itself.

boolean $showLogin

Defines if the login is optional or not.

Note

This function can not be called statically.

Example

Using different DB parameters

<?php
require_once "Auth/Auth.php";

function 
myOutput($username$status$auth)
{
    ...  
/* See first example in introduction for the full listing */
}

$params = array(
            
"dsn" => "mysql://martin:test@localhost/auth",
            
"table" => "myAuth",
            
"usernamecol" => "myUserColumn",
            
"passwordcol" => "myPasswordColumn"
            
);

$a = new Auth("DB"$params"myOutput");

$a->start();

if (
$a->getAuth()) {
    echo 
"You have been authenticated successfully.";
}
?>

This example shows you how you can specifiy alternative names for the database table and the column names. In our example, we use the table myAuth, select the username from the field myUserColumn and the password from the field myPasswordColumn. The default values for this fields are auth, username and password. Note that you can also specify an existing DB object with the dsn parameter instead of a DSN.

This feature is necessary if you want to use PEAR::Auth with a database layout that is different from the one PEAR::Auth uses by default.

Example

Using different DB parameters

<?php
require_once "Auth/Auth.php";

function 
myOutput($username$status)
{
    ...  
/* See first example in introduction for the full listing */
}

$a = new Auth(new CustomAuthContainer($options), null"myOutput");

$a->start();

if (
$a->getAuth()) {
    echo 
"You have been authenticated successfully.";
}
?>

This example shows you how you can pass your own storage container to Auth.

If the storage containers supplied with Auth are not capable of fulfilling your requirements, you can easliy create your own storage container. Read the storage containers section for more info Introduction - The storage drivers

predefined constants (Previous) add a new user (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: michael@oksofar.com
You may also want to create an Auth object without triggering a login. You would do this when the user has already logged in and you want some data about that user.

For example, to get the user name in some page other than the login page:

$auth = new Auth('null', null);
$user = $auth->getUsername();

This code retrieves the user name from the session cookie.