2022-08-26 10:48:04 - 米境通
magento注册页面添加验证码
captcha
一:app/code/local/Mage/Customer/controllers/CapthchasController.php
Header("Content-type:image/PNG");
classMage_Customer_CapthchasControllerextendsMage_Core_Controller_Front_Action
{
publicfunctiongetCode($num=4,$w=6,$h=20){
//header("Content-type:text/html;charset=utf-8");
session_start();
//去掉了01Ol等
$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);
}
publicfunctionindexAction(){
$this->getCode(4,60,20);
}
}
二:appdesignfrontenddefaultdefault emplatepersistentcustomerform
$base_url=$this->getBaseUrl();
if(strstr($base_url,'index.php')=='index.php/'){
$base_url=str_replace('index.php/','',$base_url);
}