magento 注册页面添加验证码
captcha
一:app/code/local/Mage/Customer/controllers/CapthchasController.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);
}
看不清
换一张
三: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;
}
(责任编辑:最模板)