08 Jan
Posted by Ganesh H S , Bangalore, India as zend framework
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(32) 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
<?php
require_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($authAdapter);
if ($result->isValid())
{
// success : store database row to auth's storage system
// (not the password though!)
$data = $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’); -
$authAdapter->setIdentityColumn(’user_email’); -
$authAdapter->setCredentialColumn(’user_password); -
7 Responses
tan
April 25th, 2008 at 3:58 am
1$authAdapter->setCredentialColumn(’user_password); miss ‘
Ganesh H S , Bangalore, India
April 25th, 2008 at 4:01 am
2tan-
thanks, i fixed it.
argos
October 24th, 2008 at 8:11 pm
3That was the most easiest tutorial of Zend Framework, but was excelent! Thanks man.
radu
January 3rd, 2009 at 3:49 pm
4Hello, you use $this->authAdapter in two places: line 37 and 49 and it should be $authAdapter instead of $this->authAdapter.
Good and helpful tutorial though 
Ganesh H S , Bangalore, India
January 19th, 2009 at 10:35 pm
5radu,
thanks for pointing it out, i have updated the above post.
Jeff Shaikh
February 9th, 2009 at 3:33 am
6Ganesh,
Line 43
$data = $authAdapter->getResultRowObject(null, ‘password’);
should read
$data = $authAdapter->getResultRowObject(null, ‘user_password’);
user_password is the credential column. If you list ‘password’ in the getResultrowObject method, then the user_password will be stored in the session.
Aaron
April 28th, 2009 at 9:22 pm
7Hi, great tutorial.
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Blogroll
Recent Comments
Ganesh H S is proudly powered by WordPress - BloggingPro theme by: Design Disease