php验证码zhuc_php实现验证码制作

php实现验证码制作

验证码分为:数字验证码,字母验证码,数字加字母验证码,图片验证码,汉子验证码,视频验证码等!由于原理相同,且根据平时的使用范围来看,今天在这里只讲数字验证码,字母验证码,数字加字母验证码。下面是由百分网小编为大家整理的php实现验证码制作,喜欢的可以收藏一下!了解更多详情资讯,请关注应届毕业生考试网!

首先,看一张图了解验证码生成的过程。

(1)生成验证码底图

(2)验证码内容

(3)生成验证码

(4)对比校验

验证码实现的核心技术分析

(a)底图的`实现,并添加干扰元素

(b)生成验证内容

(c)验证内容保存在服务端

(d)验证内容的校验

下面看代码实现的过程

/*

这段代码 实现了产生 随机数字,随机数字+字母验证码

*/

session_start();

$image = imagecreatetruecolor(100, 30);

//imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。

$bgcolor = imagecolorallocate($image, 255, 255, 255);

// imagecolorallocate — 为一幅图像分配颜色

imagefill($image,0,0,$bgcolor);

/*   生成字母验证码

for($i=0;$i<4;$i++)

{

$fontsize = 6;

// $fontcolor = imagecolorallocate($image, 0, 0, 0);

$fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));

$fontcontent =rand(0,9);

$x = ($i*100/4) + rand(5,10);

$y = rand(5,10);

// imagestring — 水平地画一行字符串

imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);

}

*/

// 生成字母加数字的随机验证码

$captcha_code = "";

for($i=0;$i<4;++$i)

{

$fontsize = 6;

$fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));

$data = "abcdefghijklmnopqrstuvwxtz123456789";

$fontcontent = substr($data,rand(0,strlen($data)),1);

$captcha_code.=$fontcontent;

$x = ($i*100/4) + rand(5,10);

$y = rand(5,10);

imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);

}

$_SESSION['authcode']=$captcha_code;

//  给验证码添加点干扰项

for($i=0;$i<200;$i++)

{

$pointcolor = imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200));

imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);

// bool imagesetpixel ( resource $image , int $x , int $y , int $color );

// imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。

}

// 增加线的干扰

for($i=0;$i<3;++$i)

{

$linecolor = imagecolorallocate($image, rand(80,220), rand(80,220), rand(80,220));

imageline($image, rand(1,99), rand(1,29), rand(1,99), rand(1,29), $linecolor);

}

header("Content-Type: image/png");

imagepng($image);

imagedestroy($image);

?>

【php实现验证码制作】相关文章:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值