当前位置:首页 > 代码 > 正文

php图形验证码代码(php实现验证码)

admin 发布:2022-12-19 03:38 109


今天给各位分享php图形验证码代码的知识,其中也会对php实现验证码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

PHP写的图形验证码乱码

我吧我的给你用吧

class wxhVerify {

static function imageVerify($length=4, $mode=1, $width=48, $height=22, $verifyName='verify') {

$randval = randomString($length, $mode);

$_SESSION[$verifyName] = md5($randval);

$width = ($length * 10 + 10) $width ? $length * 10 + 10 : $width;

$im = @imagecreatetruecolor($width, $height);

$r = Array(225, 255, 255, 223);

$g = Array(225, 236, 237, 255);

$b = Array(225, 236, 166, 125);

$key = mt_rand(0, 3);

//随机背景色

$backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);

//边框色

$borderColor = imagecolorallocate($im, 100, 100, 100);

//点颜色

$pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

@imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);

@imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);

$stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));

//干扰

for ($i = 0; $i 10; $i++) {

$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);

}

for ($i = 0; $i 25; $i++) {

$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);

}

for ($i = 0; $i $length; $i++) {

imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);

}

header('Content-type: image/png');

imagepng($im);

imagedestroy($im);

exit;

}

}

//生成随机字符串

function randomString($len=6, $type=1, $addChars='') {

switch ($type) {

case 0:

$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars;

break;

case 1:

$chars = str_repeat('0123456789', 3);

break;

case 2:

$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;

break;

case 3:

$chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;

break;

default :

//默认去掉了容易混淆的字符oOLl和数字01,要增加请使用addChars参数

$chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars;

break;

}

if ($len 10) {

//位数过长重复字符串一定次数

$chars = $type == 1 ? str_repeat($chars, $len) : str_repeat($chars, 5);

}

$chars = str_shuffle($chars);

return substr($chars, 0, $len);

}

调用 wxhVerify::imageVerify();

php验证码怎么实现

1. 新建code.php验证码生成文件

在此之前必须打开php的GD库,修改php.ini文件的配置,取消extension=php_gd2.dll前面的分号。代码如下:

?php

session_start();

//生成验证码图片

Header("Content-type: image/PNG");

$im = imagecreate(44,18);

$back = ImageColorAllocate($im, 245,245,245);

imagefill($im,0,0,$back); //背景

srand((double)microtime()*1000000);

//生成4位数字

for($i=0;$i4;$i++){

$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));

$authnum=rand(1,9);

$vcodes.=$authnum;

imagestring($im, 5, 2+$i*10, 1, $authnum, $font);

}

for($i=0;$i100;$i++) //加入干扰象素

{

$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($im, rand()p , rand()0 , $randcolor);

}

ImagePNG($im);

ImageDestroy($im);

$_SESSION['Checknum'] = $vcodes;

?

2. 显示验证码图片

在需要显示验证码的页面中加入

input type="text" name="passcode"

img src="code.php"

3.判断并获取验证码的值

验证码是通过第一步骤代码中的$_SESSION['Checknum'] = $vcodes;赋的值,所以验证码的值存在$_SESSION['Checknum']当中。在验证页面,使用以下代码,

...

session_start();//启动会话

$code=$_POST["passcode"];

if( $code == $_SESSION["Checknum"])

{...}即可完成验证码登录。

运行截图:

望采纳,谢谢

php的图片验证码代码

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

?php

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, 255), rand(0, 20), rand(0, 10)),

imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))

);

$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80); //分配颜色并说明透明度

$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160)); //中间背景

$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80); //中间背景2

//与左上角的颜色相同的都会被填充

imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));

//往图像上写入文字

imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[0]);

imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ARIALNI.TTF', $string[1]);

imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[2]);

imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/arial.ttf', $string[3]);

//画边框

$border = imagecolorallocate($im, 133, 153, 193);

imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);

//画一些随机出现的点

$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));

for ($i=0;$i80;$i++)

{

imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);

}

//画随机出现的线

for ($x=0; $x9;$x++)

{

if(mt_rand(0,$x)%2==0)

{

imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); //画线

imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2); //画椭圆

}

else

{

imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));

imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);

}

}

//output to browser

header("content-type:image/png\r\n");

imagepng($im);

imagedestroy($im);

}

else

{

$files = glob(XINCHENG_ROOT.'images/checkcode/*.jpg');

if(!is_array($files)) die('请检查文件目录完整性:/images/checkcode/');

$checkcodefile = $files[rand(0, count($files)-1)]; //随机其中一个文件

$_SESSION['checkcode'] = substr(basename($checkcodefile), 0, 4); //获得文件名

header("content-type:image/jpeg\r\n");

include $checkcodefile;

}

?

php图片验证码实现

可以用php的GD库做

//随机生成验证码

class randomString

{

function createRandomStr($strLen)

{

list($usec, $sec) = explode(' ', microtime());

(float) $sec + ((float) $usec * 100000);

$number = '';

$number_len = $strLen;

$stuff = '1234567890abcdefghijklmnopqrstuvwxyz';//附加码显示范围ABCDEFGHIJKLMNOPQRSTUVWXYZ

$stuff_len = strlen($stuff) - 1;

for ($i = 0; $i $number_len; $i++) {

$number .= substr($stuff, mt_rand(0, $stuff_len), 1);

}

return $number;

}

}

通过ZD库将验证码变成图片

$number = $createStr-createRandomStr('4');//验证码的位数

$number_len = strlen($number);

$_SESSION["VERIFY_CODE"] = $number;

// 生成验证码图片

$img_width = 60;

$img_height = 20;

$img = imageCreate($img_width, $img_height);

ImageColorAllocate($img, 0x6C, 0x74, 0x70);

$white = ImageColorAllocate($img, 0xff, 0xff, 0xff);

$ix = 6;

$iy = 2;

for ($i = 0; $i $number_len; $i++) {

imageString($img, 5, $ix, $iy, $number[$i], $white);

$ix += 14;

}

for($i=0;$i200;$i++) //加入干扰象素

{

$randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($img, rand()%100 , rand()%50 , $randcolor);

}

// 输出图片

header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));

imagepng($img);

imagedestroy($img);

php图形验证码代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php实现验证码、php图形验证码代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/1799.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载