Zend Auth is easy to set up and provides a system that secures our site with an easy to use authentication mechanism.

Zend Auth(Zend_Auth) provides an API for authentication and includes concrete authentication adapters for common use case scenarios.

In this Zend Authentication example, i will be discussing only about zend authenticating with a Database Table using Zend_Auth_Adapter_DbTable.

Create a table ‘users‘ in ‘test‘ database. This table will be used for authentication.

CREATE TABLE users(user_id INT AUTO_INCREMENT NOT NULL,

user_email  varchar(50) NOT NULL,

user_password varchar(25) NOT NULL,

PRIMARY KEY(user_id)

);

INSERT INTO users(user_email, user_password) VALUES('me@ganeshhs.com', '34819d7beeabb9260a5c854bc85b3e44')

Lets write the script which uses zend auth for authentication

<?phprequire_once “Zend_Loader.php”;

require_once 'Zend/Db.php';Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');

$db = Zend_Db::factory('Pdo_Mysql', array(

'host'     => 'localhost',

'username' => 'root',

'password' => 'password',

'dbname'   => 'test'

));

$authAdapter  = new Zend_Auth_Adapter_DbTable($db);

$authAdapter->setTableName('users');

$authAdapter->setIdentityColumn('user_email');

$authAdapter->setCredentialColumn('user_password');

$username = "me@ganesh.com";

$password = "mypassword";

// Set the input credential values to authenticate against

$authAdapter->setIdentity($username);

$authAdapter->setCredential(md5($password));

$auth = Zend_Auth::getInstance();

$result = $auth->authenticate($this->authAdapter);

if ($result->isValid())

{

// success : store database row to auth's storage system

// (not the password though!)

$this->consumer->email = $username;

$data = $this->authAdapter->getResultRowObject(null, 'password');

$auth->getStorage()->write($data);

}

else

{

echo "Invalid user";

}

$authAdapter = new Zend_Auth_Adapter_DbTable($db);
Creates new instance of zend auth adapter.

$authAdapter->setTableName(’users’); -

  • This sets the name of the database table that contains the authentication credentials, and against which the database authentication query is performed.
  • In this zend auth tutorial, users is the table which contains the authentication credentials, user email (users.user_email) and password (users.user_email)

$authAdapter->setIdentityColumn(’user_email’); -

  • This sets the name of the database table column used to represent the identity. The identity column must contain unique values, such as a username or e-mail address.
  • In this zend authentication example, users.user_email is the column which represent the unique identity.

$authAdapter->setCredentialColumn(’user_password); -

  • This sets the name of the database table column used to represent the credential. Under a simple identity and password authentication scheme, the credential value corresponds to the password. See also the credentialTreatment option.
  • In this zend authentication tutorial, users.user_password is the column which represent password credential.
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Simpy
  • StumbleUpon
  • Technorati
  • YahooMyWeb