PHP验证码的产生原理和实现
2012-08-12 11:25
399 查看
今天在phpchina上看到了有关php验证码的产生原理和实现,觉得不错,贴过来研究了下。
由于注册的时候常常会用到注册码来防止机器恶意注册,这里我发表一个产生png图片验证码的基本图像,很简陋但思想很清晰:
1、产生一张png的图片
2、为图片设置背景色
3、设置字体颜色和样式
4、产生4位数的随机的验证码
5、把产生的每个字符调整旋转角度和位置画到png图片上
6、加入噪点和干扰线防止注册机器分析原图片来恶意注册
7、输出图片
8、释放图片所占内存
authcode.php文件
session_start();
header('Content-type:image/png');
//创建图片
$im = @imagecreate(130,45)or die("Cannot Initialize new GD image stream");
$bg = imagecolorallocate($im,rand(50,200),rand(0,155),rand(0,155));
//第一次对imagecolorallocate()的调用会给基于调色板的图像填充背景色
$fontColor = imageColorAllocate($im,255,255,255); //字体颜色
//$fontstyle = 'Roman.ttf';
$fontstyle = 'C:\WINDOWS\Fonts\arial.ttf';
//字体样式,这个可以从C:\windows\Fonts\文件夹下找到,我把它放到和Authcode,php文件同一个目录,这里可以替换其他的字体样式
//产生随机字符
for($i=0;$i< 4;$i++)
{
$randAsciiNumArray = array(rand(48,57),rand(65,90));
$randAsciiNum = $randAsciiNumArray[rand(0,1)];
$randStr = chr($randAsciiNum);
imagettftext($im,30,rand(0,20)-rand(0,25),5+$i*30,rand(30,35),$fontColor,$fontstyle,$randStr);
$authcode .= $randStr;
}
//用户和用户输入验证码做比较
$_SESSION['authcode'] = $randFourStr;
//干扰线
for($i=0;$i<8;$i++)
{
$lineColor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imageline($im,rand(0,130),0,rand(0,130),145,$lineColor);
}
//干扰点
for($i=0;$i<250;$i++)
{
imagesetpixel($im,rand(0,130),rand(0,145),$fontColor);
}
imagepng($im);
imagedestroy($im);
由于注册的时候常常会用到注册码来防止机器恶意注册,这里我发表一个产生png图片验证码的基本图像,很简陋但思想很清晰:
1、产生一张png的图片
2、为图片设置背景色
3、设置字体颜色和样式
4、产生4位数的随机的验证码
5、把产生的每个字符调整旋转角度和位置画到png图片上
6、加入噪点和干扰线防止注册机器分析原图片来恶意注册
7、输出图片
8、释放图片所占内存
authcode.php文件
session_start();
header('Content-type:image/png');
//创建图片
$im = @imagecreate(130,45)or die("Cannot Initialize new GD image stream");
$bg = imagecolorallocate($im,rand(50,200),rand(0,155),rand(0,155));
//第一次对imagecolorallocate()的调用会给基于调色板的图像填充背景色
$fontColor = imageColorAllocate($im,255,255,255); //字体颜色
//$fontstyle = 'Roman.ttf';
$fontstyle = 'C:\WINDOWS\Fonts\arial.ttf';
//字体样式,这个可以从C:\windows\Fonts\文件夹下找到,我把它放到和Authcode,php文件同一个目录,这里可以替换其他的字体样式
//产生随机字符
for($i=0;$i< 4;$i++)
{
$randAsciiNumArray = array(rand(48,57),rand(65,90));
$randAsciiNum = $randAsciiNumArray[rand(0,1)];
$randStr = chr($randAsciiNum);
imagettftext($im,30,rand(0,20)-rand(0,25),5+$i*30,rand(30,35),$fontColor,$fontstyle,$randStr);
$authcode .= $randStr;
}
//用户和用户输入验证码做比较
$_SESSION['authcode'] = $randFourStr;
//干扰线
for($i=0;$i<8;$i++)
{
$lineColor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imageline($im,rand(0,130),0,rand(0,130),145,$lineColor);
}
//干扰点
for($i=0;$i<250;$i++)
{
imagesetpixel($im,rand(0,130),rand(0,145),$fontColor);
}
imagepng($im);
imagedestroy($im);
相关文章推荐
- PHP验证码生成原理和实现
- 全面解析PHP验证码的实现原理 附php验证码小案例
- PHP验证码生成原理和实现
- php验证码的实现原理及引入第三方类实现
- 全面解析PHP验证码的实现原理 附php验证码小案例
- php 验证码的实现原理
- php验证码实现原理
- Atitit. servlet 与 IHttpHandler ashx listen 和HttpModule的区别与联系 原理理论 架构设计 实现机制 java php c#.net j
- PHP实现队列及队列原理
- php实现动态随机验证码机制(CAPTCHA)
- 用PHP实现验证码功能(一)
- PHP session的实现原理
- 10 Memcached 一致性哈希分布式算法原理与实现[PHP实现]
- 后台登录(包含验证码)的php代码实现
- php产生简单的验证码
- 手机短信接收验证码的实现原理:
- php教程 CURL实现带有验证码网站的模拟登录的方法
- PHP实现队列的原理
- 【PHP】验证码实现
- PHP实现验证码校验功能