Ⅰ phpcmsPHPCMS v9 自定义表单添加验证码验证
要在PHPCMS v9系统中自定义表单添加验证码验证,首先需要在头部代码中加入以下代码:
pc_base::load_sys_class('form', '', 0);
这行代码的作用是加载系统中的表单类库。接下来,需要在表单生成代码中加入验证码的相关代码。具体实现步骤如下:
1. 在需要添加验证码的表单页面中引入验证码生成类库:
pc_base::load_sys_class('verify', '', 0);
2. 在表单生成代码中,添加生成验证码图片的代码:
$verify = new Verify();
$verify->entry();
3. 在表单提交处理代码中,增加验证用户输入的验证码是否正确:
if(!preg_match('/^\d{4}$/', $_POST['verifycode'])){
showmsg('验证码错误!', '', -1);
}
4. 确保在表单中添加验证码输入框和图片显示:
<img src="getCheckCodeUrl(); ?>" alt="验证码">
通过以上步骤,你就可以在PHPCMS v9的自定义表单中成功添加验证码验证功能,从而提高表单的安全性。
Ⅱ PHP如何生成加减算法方式的验证码
<?php
namespace mobile\components;
/**
* @author fenghuo
*
* 改造的加减法验证类
* 使用示例 VerifyCode::get(1,2);
* 验证示例 VerifyCode::check($code);
*/
class VerifyCode
{
/**
* php验证码
*/
public static function get($one,$two,$prefix = '', $font_size = 28)
{
//文件头...
ob_get_clean();
header("Content-type: image/png;charset=utf-8;");
//创建真彩色白纸
$width = $font_size*5;
$height = $font_size+1;
$im = @imagecreatetruecolor($width, $height) or die("建立图像失败");
//获取背景颜色
$background_color = imagecolorallocate($im, 255, 255, 255);
//填充背景颜色
imagefill($im, 0, 0, $background_color);
//获取边框颜色
$border_color = imagecolorallocate($im, 200, 200, 200);
//画矩形,边框颜色200,200,200
imagerectangle($im,0,0,$width - 1, $height - 1,$border_color);
//逐行炫耀背景,全屏用1或0
for($i = 2;$i < $height - 2;$i++) {
//获取随机淡色
$line_color = imagecolorallocate($im, rand(200,255), rand(200,255), rand(200,255));
//画线
imageline($im, 2, $i, $width - 1, $i, $line_color);
}
//设置印上去的文字
$firstNum = $one;
$secondNum = $two;
$actionStr = $firstNum > $secondNum ? '-' : '+';
//获取第1个随机文字
$imstr[0]["s"] = $firstNum;
$imstr[0]["x"] = rand(2, 5);
$imstr[0]["y"] = rand(1, 4);
//获取第2个随机文字
$imstr[1]["s"] = $actionStr;
$imstr[1]["x"] = $imstr[0]["x"] + $font_size - 1 + rand(0, 1);
$imstr[1]["y"] = rand(1,5);
//获取第3个随机文字
$imstr[2]["s"] = $secondNum;
$imstr[2]["x"] = $imstr[1]["x"] + $font_size - 1 + rand(0, 1);
$imstr[2]["y"] = rand(1, 5);
//获取第3个随机文字
$imstr[3]["s"] = '=';
$imstr[3]["x"] = $imstr[2]["x"] + $font_size - 1 + rand(0, 1);
$imstr[3]["y"] = 3;
//获取第3个随机文字
$imstr[4]["s"] = '?';
$imstr[4]["x"] = $imstr[3]["x"] + $font_size - 1 + rand(0, 1);
$imstr[4]["y"] = 3;
//文字
$text = '';
//写入随机字串
for($i = 0; $i < 5; $i++) {
//获取随机较深颜色
$text_color = imagecolorallocate($im, rand(50, 180), rand(50, 180), rand(50, 180));
$text .= $imstr[$i]["s"];
//画文字
imagechar($im, $font_size, $imstr[$i]["x"], $imstr[$i]["y"], $imstr[$i]["s"], $text_color);
}
session_start();
$_SESSION[$prefix.'verifycode'] = $firstNum > $secondNum ? ($firstNum - $secondNum) : ($firstNum + $secondNum);
//显示图片
ImagePng($im);
//销毁图片
ImageDestroy($im);
}
public static function check($code)
{
if(trim($_SESSION[$prefix.'verifycode']) == trim($code)) {
return true;
} else {
return false;
}
}
Ⅲ 如何用PHP生成验证码
PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中。PHP生成验证码的大致流程有:
1、产生一张png的图片;
2、为图片设置背景色;
3、设置字体颜色和样式;
4、产生4位数的随机的验证码;
5、把产生的每个字符调整旋转角度和位置画到png图片上;
6、加入噪点和干扰线防止注册机器分析原图片来恶意破解验证码;
7、输出图片;
8、释放图片所占内存。
session_start();
getCode(4,60,20);
functiongetCode($num,$w,$h){
$code="";
for($i=0;$i<$num;$i++){
$code.=rand(0,9);
}
//4位验证码也可以用rand(1000,9999)直接生成
//将生成的验证码写入session,备验证时用
$_SESSION["helloweba_num"]=$code;
//创建图片,定义颜色值
header("Content-type:image/PNG");
$im=imagecreate($w,$h);
$black=imagecolorallocate($im,0,0,0);
$gray=imagecolorallocate($im,200,200,200);
$bgcolor=imagecolorallocate($im,255,255,255);
//填充背景
imagefill($im,0,0,$gray);
//画边框
imagerectangle($im,0,0,$w-1,$h-1,$black);
//随机绘制两条虚线,起干扰作用
$style=array($black,$black,$black,$black,$black,
$gray,$gray,$gray,$gray,$gray
);
imagesetstyle($im,$style);
$y1=rand(0,$h);
$y2=rand(0,$h);
$y3=rand(0,$h);
$y4=rand(0,$h);
imageline($im,0,$y1,$w,$y3,IMG_COLOR_STYLED);
imageline($im,0,$y2,$w,$y4,IMG_COLOR_STYLED);
//在画布上随机生成大量黑点,起干扰作用;
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,12);
}
imagepng($im);//输出图片
imagedestroy($im);//释放图片所占内存
}
Ⅳ php怎么写短信接口
短信接口可以广泛应用在网站会员手机验证、订单通知、物流提醒等触发类短信应用。技术实现1、用户填写自己的手机号码,点击“获取验证码”按钮2、网站按规则生成短信验证码,并将用户的手机号码和验证码内容通过短信验证接口发送到验证码短信平台。3、验证码短信平台对经过一系列的判断并通过之后(账户余额、内容是否合规、手机号码是否合规、手机号码所属的运营商),将信息提交到相应运营商的服务器。4、运营商将短信内容下发到用户的手机。
Ⅳ thinkphp 怎么修改默认验证码
修改 ThinkPHP 默认验证码类的猛谨字体及大小
是否感觉ThinkPHP中的默认生成的验猛笑证码太小了?修改验证码的大小后,发现字体大小跟位置 没变化,看上去感觉很不好,下面就来改改吧…
找到ThinkPHP框架目录下的Extend/Library/ORG/Util/Image.class.php类文件打开,找到 buildImageVerify方法,大概在371行开始,用以下代码替换掉以前的(改的东西并不多,只要 加上两行代码就可以了,但为了清楚那两行代码是加在哪贴出buildImageVerify的整个方法)
1 static function buildImageVerify($length=4, $mode=1, $type='png', $width=48, $height=22,$verifyName='verify') {
2 import('ORG.Util.String');
3 $randval = String::randString($length, $mode);
4 session($verifyName, md5($randval));
5 $width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
6 if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
7 $im = imagecreatetruecolor($width, $height);
8 } else {
9 $im = imagecreate($width, $height);
10 }
11 $r = Array(225, 255, 255, 223);
12 $g = Array(225, 236, 237, 255);
13 $b = Array(225, 236, 166, 125);
14 $key = mt_rand(0, 3);
15
16 $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]); //背景色(随机)
17 $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
18 imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
19 imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
20 $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
21 // 干扰枝知基
22 for ($i = 0; $i < 10; $i++) {
23 imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor);
24 }
25 for ($i = 0; $i < 25; $i++) {
26 imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor);
27 }
28 $fontPath=THINK_PATH."/Common/ERASDEMI.TTF"; //字体文件路径,可自行更改,我放在ThinkPHP目录下的Common目录里面
29 for ($i = 0; $i < $length; $i++) {
30 //imagestring($im, 5, $i * 18 + 5, mt_rand(1, 8), $randval{$i}, $stringColor); //这是默认的
31 imagettftext($im, 20, mt_rand(-30,30) , $i * 16 + 5, $height/1.4, $stringColor, $fontPath,$randval{$i}); //这个是新的,用imagettftext函数
32 }
33 Image::output($im, $type);
34 }