第一:搭建项目
项目名 captcha
Window+R启动 运行窗口,输入 cmd,启动Dos
>d:
>cd d:\wamp\www\test
>zf show version
>zf create project captcha
创建项目 captcha完毕。
第二:虚拟主机路径
http-vhosts.conf文件添加
#==================test captcha=================#
<VirtualHost *:80>
DocumentRoot "D:/wamp/www/test/captcha/public"
ServerName captcha.test.com
# This should be omitted in the production environment
SetEnv APPLICATION_ENV development
<Directory "D:/wamp/www/test/captcha/public">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
hosts文件添加
#=============test captcha======================#
127.0.0.1 captcha.test.com
浏览器直接 captcha.test.com指向captcha项目
第三:修改项目
captcha->application目录修改
改为modules模式(模块模式,分模块清晰)
modules->default->controllers
modules->default->views (default是模块名)
删除原先的controllers 、views
修改配置文件application.ini
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
改为:
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
第四:配置 静态文件目录地址常量
application.ini添加
constants.STATIC_SITE = http://captcha.test.com
因为静态文件放置在public目录下,而http://captcha.test.com指向public目录
Bootstrap.php解析application.ini
public function _initApplications()
{
$applications = new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini", APPLICATION_ENV);
Zend_Registry::set('applications', $applications);
return $applications;
}
public function _initConstants()
{
$constants = Zend_Registry::get('applications')->constants;
$constants = $constants ? $constants : array();
foreach($constants as $constant => $value){
defined($constant) || define($constant, $value);//defined() 函数检查某常量是否存在
}
}
第五:修改default->views->scripts->index->index.phtml文件
<label for="">验证码:</label><input type="text" class="inp_code" name='captcha'/>
<img src="<?php echo STATIC_SITE;?>/html/images/common/s.gif" class="code_img" /> <!-- <?php echo STATIC_SITE;?>静态文件目录地址 -->
<a href="#" class='changeCaptcha'>看不清?换一个</a>
现在情况如图:
第六:验证码生成
1,添加助手文件目录
library->Ata->Controller->Action->Helpers目录下
2,修改Bootstrap.php文件,使助手文件Captcha.php能正常使用
public function _initHelper()
{
Zend_Controller_Action_HelperBroker::addPrefix('Ata_Controller_Action_Helpers');
}
3,助手文件Captcha.php(验证码生成等方面)
Captcha.php(别人的代码直接拿来用的)
<?php
class Ata_Controller_Action_Helpers_Captcha extends Zend_Controller_Action_Helper_Abstract
{
public function img()
{
$session = new Zend_Session_Namespace('captcha');
$captcha = $session->content;
$img = imagecreate(120, 40);
$bg = imagecolorallocate($img, 255, 255, 128);
$fonts = array(
APPLICATION_PATH.'/../library/Ata/Fonts/arial.ttf',
APPLICATION_PATH.'/../library/Ata/Fonts/arial.ttf',
APPLICATION_PATH.'/../library/Ata/Fonts/arial.ttf',
APPLICATION_PATH.'/../library/Ata/Fonts/arial.ttf',
APPLICATION_PATH.'/../library/Ata/Fonts/fakto9.ttf',
APPLICATION_PATH.'/../library/Ata/Fonts/fakto10.ttf',
APPLICATION_PATH.'/../library/Ata/Fonts/faktos.ttf',
APPLICATION_PATH.'/../library/Ata/Fonts/fpnf.ttf'
);
$grey = imagecolorallocate($img, 128, 128, 128);
$black = imagecolorallocate($img, 0, 0, 0);
for($i=0;$i<25;$i++){
$x = rand(0,120);
$y = rand(0,40);
$size = rand(20,100);
$angle_A = rand(0,360);
$angle_B = rand(0,360);
imagearc($img, $x, $y, $size, $size, $angle_A, $angle_B, $grey);
}
for($i=0;$i<10;$i++){
$x = rand(0,120);
$y = rand(0,40);
$size = rand(3,3);
imagefilledarc($img, $x, $y, $size, $size, 0, 360, $black, IMG_ARC_PIE);
}
for($i=0;$i<strlen($captcha);$i++){
$angle = rand(-10,10);
$rand = rand(0,3);
imagettftext($img, 20, $angle, 17+25*$i, 35, $grey, $fonts[$rand], $captcha[$i]);
imagettftext($img, 20, $angle, 15+25*$i, 32, $black, $fonts[$rand], $captcha[$i]);
}
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
return '';
}
public function set(){
$chars = "ABCDEFGHKMNPQRSTWXY3456789";
$captcha = '';
for($i=0;$i<4;$i++){
$captcha.= $chars[rand(0,strlen($chars)-1)];
};
$session = new Zend_Session_Namespace('captcha');
$session->content = $captcha;
return 1;
}
public function get(){
$session = new Zend_Session_Namespace('captcha');
return $session->content;
}
public function verify($captcha){
$session = new Zend_Session_Namespace('captcha');
return strtolower($session->content)==strtolower($captcha);
}
}
3.1,用到session,配置application.ini
resources.session.name = "HRS2ATAPSID"
resources.session.gc_maxlifetime = 43200
resources.session.use_trans_sid = 0
resources.session.use_cookies = 1
resources.session.use_only_cookies = 1
resources.session.cookie_path = "/"
resources.session.remember_me_seconds = 43200
resources.session.cookie_domain = ".test.com"
memcache.server.0.host = 172.16.26.234
memcache.server.0.port = 11221
memcache.server.1.host = 172.16.26.234
memcache.server.1.port = 11222
memcache.lifetime = 43200
memcache.compressed = 0
使用memcache存储session
3.2,ttf 文件放置位置
library->Ata->Fonts目录下
第七:写控制器 CaptchaController.php来调用助手文件Captcha.php(为页面js调用准备方法)
CaptchaController.php文件
<?php
class CaptchaController extends Zend_Controller_Action
{
public function imgAction(){
echo $this->getHelper('Captcha')->img();exit;
}
public function setAction(){
echo $this->getHelper('Captcha')->set();exit;
}
public function verifyAction(){
$captcha = $this->requestParam('captcha');
echo $this->getHelper('Captcha')->verify($captcha)?'y':'n';exit;
}
}
第八:页面事件响应(刷新验证码)
加载jquery.js文件
<?php $this->headScript()->prependFile(STATIC_SITE."/html/scripts/common/jquery.js");?>
<?php echo $this->headScript();?>
写js方法
<script type="text/javascript">
$(document).ready(function(){
function setCaptchaImage(){ //set验证码图片
$.get('/captcha/set?'+Math.random(), //get请求 url Math.random()随机数
function(data){ //返回的参数
$('img.code_img').attr('src', '/captcha/img?'+Math.random()); //替换验证图片
});
}
$('.changeCaptcha').click(function(){ //点击链接切换验证码
setCaptchaImage();
return false;
});
setCaptchaImage(); //页面加载时就调用setCaptchaImage()方法,显示验证码
});
</script>
第九:最后如图