免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1138 | 回复: 0
打印 上一主题 下一主题

学习使用Zend_Auth认证登陆 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-26 17:28 |只看该作者 |倒序浏览

                看了很多别人的文章,自己总结了下,做个笔记
        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秒后实效。呵呵。
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/15722/showart_508989.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP