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(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’); -
$authAdapter->setIdentityColumn(’user_email’); -
$authAdapter->setCredentialColumn(’user_password); -
3 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.
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Blogroll
Recent Comments