看了很多别人的文章,自己总结了下,做个笔记
Zend_Auth 只涉及
认证而不是
授权。授权使用Zend_Acl明天学习学习。
1)使用ZDE(zend Studio for eclipse 6)创建一个空的end framework项目。我使用sqlite数据库存储认证信息的。动态创建表和用户,只是测试吗。
<?php
/**
* My new Zend Framework project
*
* @author
* @version
*/
set_include_path('.' . PATH_SEPARATOR . '../library' . PATH_SEPARATOR . './application/default/models/' . PATH_SEPARATOR . get_include_path());
//require_once 'Zend/Controller/Front.php';
include "Zend/Loader.php";
Zend_Loader::registerAutoload();
/**
* Setup controller
*/
$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory('../application/default/controllers');
$controller->throwExceptions(true); // should be turned on in development time
// 创建一个 in-memory SQLite 数据库连接
$dbAdapter = new Zend_Db_Adapter_Pdo_Sqlite(array('dbname' => ':memory:'));
// 构造一个简单表的创建语句
$sqlCreate = 'CREATE TABLE [users] ( '
. '[id] INTEGER NOT NULL PRIMARY KEY, '
. '[username] VARCHAR(50) UNIQUE NOT NULL, '
. '[password] VARCHAR(32) NULL, '
. '[real_name] VARCHAR(150) NULL)';
// 创建认证证书表
$dbAdapter->query($sqlCreate);
// 构造用来插入一行可以成功认证的数据的语句
$sqlInsert = 'INSERT INTO users (username, password, real_name) '
. 'VALUES ("test", "test", "My Real Name")';
// 插入数据
$dbAdapter->query($sqlInsert);
// 用构造器参数来配置实例...
//$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter, 'users', 'username', 'password');
Zend_Registry::set('authAdapter', $dbAdapter);
// run!
$controller->dispatch();
|
2)修改默认的IndexController.php
<?php
/**
* IndexController - The default controller class
*
* @author
* @version
*/
require_once 'Zend/Controller/Action.php';
class IndexController extends Zend_Controller_Action
{
protected $_auth;
protected $_userId;
protected $_userName;
public function init()
{
$this->_auth = Zend_Auth::getInstance()
->setStorage(new Zend_Auth_Storage_Session('authNameSpace'));
}
/**
* The default action - show the home page
*/
public function indexAction()
{
if ($this->_auth->hasIdentity()) {
$this->_userId = $this->_auth->getStorage()->read()-> id
$this->_userName = $this->_auth->getStorage()->read()->
username ;
//echo 'welcome ';
} else {
$this->_forward('login');
}
}
public function loginAction()
{
if ($this->_request->isPost()) {
$f = new Zend_Filter_StripTags();
$username = $f->filter($this->_request->getParam('username'));
$password = $f->filter($this->_request->getParam('password'));
//建立认证适配器 (DB适配器名/表名/用户名字段/密码字段)
$dbAdapter = Zend_Registry::get('authAdapter');
//authAdapter在index.php中注册过了
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter, 'users', 'username', 'password');
$authAdapter->setIdentity($username)
->setCredential($password);
// do the authentication
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
//$result = $this->_auth->authenticate($authAdapter);
//建立持久连接
if ($result && $result->isValid()) {
if ($this->_request->getParam('rememberMe')) { //记住密码
Zend_Session::rememberMe('315360000');//session过期时间
} else {
Zend_Session::forgetMe();
}
$this->_auth->getStorage()->write($authAdapter->getResultRowObject(array('id', 'username')));
$this->_redirect('/');
} else {
echo 'login error';
}
}
}
}
|
3)在/views/scripts/index/下创建一个login.phtml
<h1><?php echo $this->escape($this->title); ?></h1>
<form action="index/login" method="post">
<div><label for="username">Username</label> <input type="text"
name="username" value="" /></div>
<div><label for="password">Password</label> <input type="password"
name="password" value="" /><br>
<input type="checkbox" name = "rememberMe"> rememberMe </div>
<div id="formbutton"><input type="submit" name="login" value="Login" />
</div>
</form>
|
别的不做什么修改就可以测试了,用户test,密码test.不选
rememberMe复选框浏览器关闭就认证失效,选中了就在
315360000秒后实效。呵呵。
阅读(4630) | 评论(1) | 转发(1) |