Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 Zendframework登陆注册实例详解

Zendframework登陆注册实例详解

发布时间:2017-12-20   编辑:www.jquerycn.cn
分享下zendframework实现登录注册的实例教程,用zf框架实现一个登录注册的功能模块,感兴趣的朋友参考下。

本节内容:
使用Zendframework实现一个登陆注册模块。

一,服务器配置
说明,这个实例的开发系统环境是windows 7,所用服务器是Nginx1.0.5,Zendframework框架的版本是1.11.9,PHP5.3.6,所用数据库是MySQL 5.1.50
首先,从配置Nginx开始,需要开启URL的rewrite:
这个是nginx.conf文件中配置虚拟主机部分。

文件:D:\nginx\nginx-1.0.5\conf\nginx.conf
 

复制代码 代码示例:
location ~ \.php$ {
  root html;
  fastcgi_pass   127.0.0.1:9000;
 
   #如果是一个文件夹,访问规则301
   if (-d $request_filename){
       rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
   }
 
   #如果没有改文件访问默认文件
   if (!-e $request_filename) {
          rewrite ^/(.*)$ /myproject/index.php last;
         }
 
   #自动在访问URL后面加"/"
   if (-d $request_filename){
         rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
   }
 
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
   fastcgi_param PATH_INFO $fastcgi_script_name;
 
   include        fastcgi_params;

二.建立项目
然后,在Nginx的html目录下创建如下文件:
myproject/
/application
/controllers
/models
/views
/filters
/helpers
/scripts
/library
/public
/images
/scripts
/styles
 
Zend Framework 的控制器,Zend_Controller,被设计支持使用clean urls 的网站。
为实现这个目的,所有的请求需要通过单一的index.php 文件,这就是所知的启动文件(bootstrapper)。这给提供了程序中所有页面的中心点并确保运行环境配置正确。

用.htaccess 文件来实现这个目的,.htaccess 在myproject 的根目录中,内容如下:
文件:myproject/.htaccess
 

复制代码 代码示例:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
 

RewriteRule 非常简单并可以翻译为“对任何url, 重定向到index.php”。

然而,对于图像,JavaScript 和CSS 文件的请求不应该重定向到启动文件,把这些文件放到public 目录下myproject/public 下:
文件:myproject/public/.htaccess
RewriteEngine off
虽然当前的rewrite rules 不需要太严格,但还是在application 和library 目录下添加一些.htaccess 文件用来保护程序:
文件:myproject/application/.htaccess
deny from all

文件:myproject/library/.htaccess
deny from all

三.网站
启动文件
接下来开始正式开始这个项目,首先,启动文件,也就是放在myproject根目录下的index.php它是个跳转的文件,在其中告诉请求执行那些操作。
文件myproject/index.php
 

复制代码 代码示例:
<?php
error_reporting ( E_ALL | E_STRICT );
date_default_timezone_set ( 'Europe/London' );
set_include_path ( '.' . PATH_SEPARATOR . './library' . PATH_SEPARATOR . './application/models/' . PATH_SEPARATOR . get_include_path () );
 
include "Zend/Loader.php";
Zend_Loader::loadClass ( 'Zend_Controller_Front' );
Zend_Loader::loadClass ( 'Zend_Config_Ini' );
Zend_Loader::loadClass ( 'Zend_Registry' );
Zend_Loader::loadClass ( 'Zend_Db' );
Zend_Loader::loadClass ( 'Zend_Db_Table' );
Zend_Loader::loadClass ( 'Zend_Auth' );
Zend_Loader::loadClass ( 'Zend_Auth_Adapter_DbTable' );
 
$config = new Zend_Config_Ini ( './application/config.ini', 'general' );
$registry = Zend_Registry::getInstance ();
$registry->set ( 'config', $config );
 
$db = Zend_Db::factory ( $config->db->adapter, $config->db->config->toArray () );
Zend_Db_Table::setDefaultAdapter ( $db );
 
$frontController = Zend_Controller_Front::getInstance ();
$frontController->throwExceptions ( true );
$frontController->setBaseUrl ( '/login' );
$frontController->setControllerDirectory ( './application/controllers' );
 
// run!
$frontController->dispatch ();
 

注意,没有在文件的结尾放?>,因为它不是必须的并且有个好处是:
可以防止当多余的whitespace 发生在?>后面出现难以调试的错误。
 
创建模型(M)
做这个例子需要建立一个用户信息的数据库,之后在mysql里建立一个数据库,里面需要有如下数据,一个数据表user
里面含有三个字段id,username,password。
然后,在models里面建立自己的模型:
文件:myproject/application/models/user.php
 

复制代码 代码示例:
<?php
class User extends Zend_Db_Table {
   
     protected $_id;
     protected $_username;
     protected $_password;
   
     public function checkUnique($username) {
    //注册验证用户米是否存在
    $select = $this->_db->select ()->from ('user')->where ( 'username = ?', $username );
    $result = $this->getAdapter ()->fetchOne ( $select );
  
    if ($result) {
   return true;
    } else {
   return false;
    }
     }
}

连接数据库需要有配置文件,Zend Framework 提供了一个Zend_Config 来提供灵活的面向对象访问配置文件。

此刻,配置文件可以是一个PHP 数组,一个INI 文件或者XML 文件。

将使用INI 文件:
文件:myproject/application/config.ini
 

复制代码 代码示例:
[general]
db.adapter = PDO_MYSQL
db.config.host = localhost
db.config.username = root
db.config.password = 123456
db.config.dbname = login
 

创建试图文件(V)
使用Zend studio创建视图文件可以在Zend studio里面右键项目—newàotherà会弹出下面页面,建立视图文件。
 
有了控制器,就可以创建视图文件了,基于MVC模式写的Zendframework的视图文件扩展名是.phtml
文件:myproject\application\views\scripts\index.phtml
 

复制代码 代码示例:
<?php echo $this->render('header.phtml'); ?>
<h1><?php echo $this->escape($this->title); ?></h1>
<form name = "form1" action="index/login" method="post">
用户名:<input type="text" id="username"  name="username"/> <br/>    
密码:<input type="password" id="password"  name="password"/> <br/>
<?php echo $this->username;?>
<input type="submit" name="submit" value="登陆">
</form>
<a href="/register/index">还没有账号,点此注册</a>
<div style="display:none;">
<table>
测试用户名和密码,用于查看数据库是否连接成功
<?php foreach($this->user as $u) : ?>
<tr>
<td><?php echo $this->escape($u->username);?></td>
<td><?php echo $this->escape($u->password);?></td>
<td>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
 
<?php echo $this->render('footer.phtml'); ?>
 

其中考虑到代码的公共部分可以重复使用,在视图里加了头部和脚部的文件。
文件:myproject/application/views/scripts/header.phtml
 

复制代码 代码示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
Page 9 of 18
<title><?php echo $this->escape($this->title); ?></title>
</head>
<body>
<div id="content">
 
文件:myproject/application/views/scripts/footer.phtml
</div>
</body>
</html>

设置控制器(C)
 
在Zend studio里面右键项目—newàotherà会弹出下面页面,建立控制器。

假如我们把这个网站的首页设为登录界面,则在设置的默认控制器为
文件:myproject/application/controllers/IndexController.php
 

复制代码 代码示例:
<?php
require_once 'Zend/Controller/Action.php';
class RegisterController extends Zend_Controller_Action {
         function init() {
         $this->initView ();
         $this->view->baseUrl = $this->_request->getBaseUrl ();
         Zend_Loader::loadClass ( 'User' );
         }
       
         public function indexAction() {
       
         if ($this->getRequest ()->isPost ()) {
        $username = $this->_request->get ( "username" );
        //echo $username;
        $password = $this->_request->get ( "password" );
        $pwd = 'w' . $password;
        $pwd = md5 ( $pwd );
        echo $pwd;
        $user = new User ();
        $auth = Zend_Auth::getInstance ();
      
        if ($user->checkUnique ( $username )) {
       echo "用户名已经存在!";
        } else {
       $data = array ('username' => $username, 'password' => $pwd );
     
       $user->insert ( $data );
       $this->_redirect ( '/register/success' );
      
        }
       
         } else {
        $this->view->title = "欢迎注册";
        $this->render ();
         }
       
         }
       
         function logoutAction() {
         // 处理注销
         $storage = new Zend_Auth_Storage_Session ();
         $storage->clear ();
         $this->_redirect ( '/index/index' );
         }
       
         function successAction() {
         // 注册成功
         }
}
 

好了,现在在浏览器里输入你的URL就可以看到如下界面了,如果想要测试数据库连接可以把上面代码中的<div style="display:none;">的style去掉。
URL: http://127.0.0.1/myproject
 
接下来处理登录表单发送的POST请求,在action里面写了它的controller和action的名字在上面的代码中我们已经给出了,这里只做简单的说明:后台Indexaction通过接收前台发送的username和password尽心加密处理,只里面问了增加安全界别,我们在密码前面加了一个字符再用md5()函数进行加密,然后通过框架中自带的类
Zend_Auth_Adapter_DbTable中的方法进行密码比较。然后是逻辑判断之后我们会增加登录成功的页面和出错的页面:
文件:myproject\application\views\scripts\index\login.phtml
 

复制代码 代码示例:
<?php
echo "<h1>恭喜你登陆成功!</h1>";
 

文件:myproject\application\views\scripts\index\error.phtml
 

复制代码 代码示例:
<?php
echo "用户名或者密码错误!";
 

以及一些CSS的文件:
文件:myproject /public/styles/site.css
 

复制代码 代码示例:
body,html {
font-size:100%;
margin: 0;
font-family: Verdana,Arial,Helvetica,sans-serif;
color: #000;
background-color: #fff;
}
h1 {
font-size:1.4em;
color: #800000;
background-color: transparent;
}
#content {
width: 770px;
margin: 0 auto;
}
label {
width: 100px;
display: block;
float: left;
}
#formbutton {
margin-left: 100px;
}
a {
color: #800000;
}
 

有了登录的例子,可以继续写注册了其实原理都一样,只不过使用的SQL语句有差别而已,还是先建立控制器controller和视图view文件吧.
 
注册功能的视图文件:
文件:myproject\application\views\scripts\register\index.phtml
 

复制代码 代码示例:
<?php echo $this->render('header.phtml'); ?>
<h1><?php echo $this->escape($this->title); ?></h1>
 
<form name = "form1" action="/register/index" method="post">
用户名:<input type="text" id="username"  name="username"/> <br/>    
密码:<input type="password" id="password"  name="password"/> <br/>
 
<?php echo $this->username;?>
<input type="submit" name="submit" value="注册"/>
</form>
<a href="/index/index">已有账号,点此登录</a>
<?php echo $this->render('footer.phtml'); ?>
文件:myproject\application\views\scripts\register\success.phtml
<?php
echo "注册成功!";
?>
<a href="/index/index">
<h1>点此登录</h1>
</a>

注册功能的控制器文件:
文件:myproject/application/controllers/RegisterController.php
 

复制代码 代码示例:
<?php
require_once 'Zend/Controller/Action.php';
 
class RegisterController extends Zend_Controller_Action {
   
     function init() {
    $this->initView ();
    $this->view->baseUrl = $this->_request->getBaseUrl ();
    Zend_Loader::loadClass ( 'User' );
     }
   
     public function indexAction() {
  
    if ($this->getRequest ()->isPost ()) {
   $username = $this->_request->get ( "username" );
   //echo $username;
   $password = $this->_request->get ( "password" );
   $pwd = 'w' . $password;
   $pwd = md5 ( $pwd );
   echo $pwd;
   $user = new User ();
   $auth = Zend_Auth::getInstance ();
 
   if ($user->checkUnique ( $username )) {
   echo "用户名已经存在!";
   } else {
   $data = array ('username' => $username, 'password' => $pwd );
           
   $user->insert ( $data );
   $this->_redirect ( '/register/success' );
 
   }
  
    } else {
   $this->view->title = "欢迎注册";
   $this->render ();
    }
   
     }
     function successAction() {
    // 注册成功
     }
}
 

在创建模型文件的时候可能会有checkUnique()函数有所困惑,现在可以明确了,在注册的控制器里用到的。当用户填写表单数据发送到后台之后检测用户名是否存在,不存在时候可以在数据表中插入一条数据,注意一定是以数组形式插入。
最后看一下这个网站的目录结构了:
 

myproject/
/application
/controllers
         /IndexController.php
         /RegisterController.php
/models
         /User.php
/views
/filters
/helpers
/scripts
         /index
        /index.phtml
        /Login.phtml
        /error.phtml
/register
         /index.phtml
         /success.hhtml
/footer.phtml
/header.phtml
        /.htaccess
        /config.ini
/library
    /Zend…(此处省略一万字…)
    /.htaccess
/public
/images
/scripts
/styles
         /site.css
/.htaccess
/.htacccess

您可能感兴趣的文章:
Zendframework登陆注册实例详解
php 判断用户是否登录
PHP Session 变量用法详解与实例教程
阿里旺旺无法登录什么原因?阿里旺旺登录不了解决方法
IE8浏览器网上银行登陆不了(建行/农行/广发)解决办法
12306网站登录密码忘记找回方法
php判断使用什么框架
【PHP】短信接口(正则匹配)
微信支付接口开发新手常见问题解决
php实现注册登录的完整程序(使用mysql数据库)

关键词: zf框架  登录   
[关闭]