当前位置:Gxlcms > PHP教程 > PHP制作图形验证码代码分享,php图形验证码代码_PHP教程

PHP制作图形验证码代码分享,php图形验证码代码_PHP教程

时间:2021-07-01 10:21:17 帮助过:5人阅读

PHP制作图形验证码代码分享,php图形验证码代码


效果:

myvcode.class.php:封装创建验证码的类

<?php
/*
* file:myvcode.class.php
* 验证码类,类名Vcode
*/
class Vcode
{
private $width; /*验证码宽度*/
private $height; /*验证码高度*/
private $codeNum; /*验证码字符个数*/
private $checkCode; /*验证码字符*/
private $image; /*验证码资源*/
private $pixNum; /*绘制干扰点的个数*/
private $lineNum; /*绘制干扰线的条数*/
/*
*构造方法实例化验证码对象,并初始化数据
*@param int $width 设置默认宽度
*@param int $height 设置默认高度
*@param int $codeNum 设置验证码中的字符个数
*@param int $pixNum 设置干扰点的个数
*@param int $lineNum 设置干扰线的数量
*/
function __construct($width=80,$height=40,$codeNum=4,$pixNum=40,$lineNum=5)
{
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
$this->pixNum = $pixNum;
$this->lineNum = $lineNum;
}
/*内部私有方法,创建图像资源*/
private function getCreateImage()
{
$this->image = imagecreatetruecolor($this->width, $this->height);
$white = imagecolorallocate($this->image,0xff,0xff,0xff);
imagefill($this->image, 0, 0, $white);
$black = imagecolorallocate($this->image,0,0,0);
imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $black);
}
/*内部私有方法,绘制字符,去掉o0Llz和012*/
private function createCheckCode()
{
$code = '3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY';
$this->checkCode = "";
for($i=0; $i<$this->codeNum;$i++)
{
$char = $code{rand(0,strlen($code) - 1)};
$this->checkCode .= $char;
$fontColor = imagecolorallocate($this->image, rand(0,128), rand(0,128),rand(0,128));
$fontSize = rand(3,5);
$x = rand(0,$this->width-imagefontwidth($fontSize));
$y = rand(0,$this->height-imagefontheight($fontSize));
imagechar($this->image, $fontSize, $x, $y, $char, $fontColor);
}
}
/*内部私有方法设置干扰元素*/
private function setDisturbColor()
{
/*绘制干扰点*/
for($i=0; $i<$this->pixNum; $i++)
{
$color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);
}
/*绘制干扰线*/
for($i=0; $i<$this->lineNum; $i++)
{
$color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
imageline($this->image, rand(1,$this->width / 2), rand(1,$this->height / 2),
rand($this->width / 2,$this->width – 2), rand($this->height / 2,$this->height – 2), $color);
}
}
/*开启session保存 利用echo
输出图像*/
function __toString()
{
$_SESSION['code'] = strtoupper($this->checkCode);
$this->getCreateImage();
$this->createCheckCode();
$this->setDisturbColor();
$this->outputImg();
}
/*内部私有方法输出图像*/
private function outputImg()
{
header("content-type:image/png");
imagepng($this->image);
}
/*析构方法,释放对象*/
function __destruct()
{
imagedestroy($this->image);
}
}
?>

https://www.gxlcms.com/imgcode.php输出图像

<?php
session_start();
require_once('myvcode.class.php');
echo new Vcode();
?>

test.html:同过img标签引用


可以加一个a标签,用js实现换一张效果:

/*局部刷新换验证码*/
function changeCode()
{
var imgcode = document.getElementById(‘code');
var change = document.getElementById(‘change');
change.onclick = function()
{
/*必须加后面的参数才能刷新*/
imgcode.src='code.php?tm'+Math.random();
}
}

code和change分别是img和a的id


php的图片验证码代码

这个是phpcms的验证码,经过十几万个网站经验的,非常好用

session_start();

$enablegd = 1;
//判断图像处理函数是否存在
$funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellipse','imagepng');
foreach($funcs as $func)
{
if(!function_exists($func))
{
$enablegd = 0;
break;
}
}

ob_clean(); //清理缓冲

if($enablegd)
{
//create captcha
$consts = 'cdfgkmnpqrstwxyz23456';
$vowels = 'aek23456789';
for ($x = 0; $x < 6; $x++)
{
$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //获取$consts中的一个随机数
$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //获取$vowels中的一个随机数
}
$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
$_SESSION['checkcode'] = $string = substr($radomstring,0,4); //显示4个字符

$imageX = strlen($radomstring)*8; //图像的宽
$imageY = 20; //图像的高
$im = imagecreatetruecolor($imageX,$imageY); //新建一个真彩色图像

//creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im, rand(245, ......余下全文>>
 

插入php图片验证码后,递交时写判断代码??

action.php

session_start();
$password = md5(trim($_POST['password']));
$str_reg=$_POST['number']; //用户填写的验证码
$str_reg = strtoupper($str_reg); //转换大写

if ($str_reg !=$_SESSION['yzm'] or empty($str_reg) )
{echo "验证码错误";
}
else
{
$_SESSION['yzm']=""; //清除session
……
执行数据库查询操作,验证用户名,密码
}
?>
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/898291.htmlTechArticlePHP制作图形验证码代码分享,php图形验证码代码 效果: myvcode.class.php:封装创建验证码的类 php /* * file:myvcode.class.php * 验证码类,类名Vcode...

人气教程排行