服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > magento教程 >

实现magento注册页面添加验证码

时间:2015-07-10 00:56来源:未知 作者:最模板 点击:
magento 注册页面添加验证码 captcha 一:app/code/local/Mage/Customer/controllers/CapthchasController.php ?php Header(Content-type: image/PNG); class Mage_Customer_CapthchasController extends Mage_Core_Controller_Front_Action { pub

实现magento注册页面添加验证码

magento 注册页面添加验证码
 
captcha
 
一:app/code/local/Mage/Customer/controllers/CapthchasController.php
 
 
<?php
Header("Content-type: image/PNG");
class Mage_Customer_CapthchasController extends Mage_Core_Controller_Front_Action
{
 
public function getCode($num=4,$w=6,$h=20){
 
// header("Content-type:text/html;charset=utf-8");
session_start();
// 去掉了 0 1 O l 等
$str = "23456789abcdefghijkmnpqrstuvwxyz";
$code = '';
for ($i = 0; $i < $num; $i++) {
$code .= $str[mt_rand(0, strlen($str)-1)];
}
//将生成的验证码写入session,备验证页面使用
$_SESSION["helloweba_char"] = $code;
//创建图片,定义颜色值
 
$im = imagecreate($w, $h);
$black = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
$gray = imagecolorallocate($im, 118, 151, 199);
$bgcolor = imagecolorallocate($im, 235, 236, 237);
 
//画背景
imagefilledrectangle($im, 0, 0, $w, $h, $bgcolor);
//画边框
imagerectangle($im, 0, 0, $w-1, $h-1, $gray);
//imagefill($im, 0, 0, $bgcolor);
 
  
 
//在画布上随机生成大量点,起干扰作用;
for ($i = 0; $i < 80; $i++) {
imagesetpixel($im, rand(0, $w), rand(0, $h), $black);
}
//将字符随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
$strx = rand(3, 8);
for ($i = 0; $i < $num; $i++) {
$strpos = rand(1, 6);
imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black);
$strx += rand(8, 14);
}
$te=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//字体颜色
imagepng($im);
imagedestroy($im);
imagettftext($im,12,3,20,20,$te,'t1.ttf',$str);
}
public function indexAction(){
$this->getCode(4,60,20);
}
 
}
二:app\design\frontend\default\default\template\persistent\customer\form
 
 
$base_url = $this->getBaseUrl();
if(strstr($base_url,'index.php')=='index.php/'){
$base_url = str_replace('index.php/','', $base_url);
}
 
<div class="email_yzmbtn">
<span id="getmathcode" style="color:#ff389b;height:47px;line-height:25px;cursor:pointer;width:49px;margin-left:6px;margin-top:10px;display: block;float:right">看不清<br/>换一张</span>
<img style=" margin-top:11px;margin-left:10px;width: 75px; height: 46px;cursor:pointer;padding:0; float:right; vertical-align: bottom;" src="<?php echo $base_url?>customer/capthchas/" id="getcode_char" title="看不清,点击换一张" align="absmiddle">
<input type="text" name="yzm" id="yzm" class="input-line input-text required-entry validate-icode" placeholder="验证码" onfocus="if(placeholder=='验证码'){this.placeholder=''};" onblur="if(this.placeholder==''){this.placeholder='验证码'};">
</div>
 
三:js 验证
 
 
//点击 切换验证码
jQuery(function(){
jQuery("#getmathcode").click(function(){
jQuery('#getcode_char').attr("src",'<?php echo $base_url?>customer/capthchas/getVerification?' + Math.random());
});
});
 
//ajax验证码处理
 
  
 
jQuery('#submitbtn').click(function(){
jQuery('#submitbtn').text('Loading...');
var icode = false;
var code_char = jQuery("#yzm").val();
Validation.add('validate-icode', '验证码错误。', function(v) {
jQuery.ajaxSetup({
async : false
});
jQuery.post(
"<?php echo $base_url?>customer/capthchas/Verification?act=char",
{code:code_char},
function(msg){
if(msg){
v = true;
}else{
v = false;
}
}
)
if(!v){
return false;
}
return true;
});
四:注册session验证
 
 
$action = $_GET['act'];
$code = trim($_POST['code']);
 
//echo $code.'-'.$_SESSION["helloweba_char"];
if(trim($code)==$_SESSION["helloweba_char"]){
return  1;
}else{
//return 0;
}

 

 
(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容