用PHP实现验证码检测登录功能
第一步:
验证自己的PHP环境是否支持GD库,直接echo phpinfo();查找GD就晓得了。
第二步:
开始制作验证码。
<?php
//phpinfo();
session_start();
$image=imagecreatetruecolor(100,30);
$bgcolor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgcolor);
// for($i=0;$i<4;$i++){
// $fontsize = 6;
// $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($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
// }
//数字和字母的验证码
$check_verify = '';
for ($i=0; $i <4 ; $i++) {
$fontsize = 6;//字体大小
$fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
$data = 'abcdefjhijkmnprstuvwxy3456789';//随机字体数据
$fontcontent = substr($data,rand(0,strlen($data)),1);
$x = ($i*100/4) + rand(5,10);
$y = rand(5,10);
$check_verify .= $fontcontent;
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION['verify']=$check_verify;
//干扰噪点
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);
}
//干扰线
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
header("Content-type:text/html;Charset=UTF8");
if(isset($_REQUEST['verify'])){
session_start();
if (strtolower($_REQUEST['verify'])==$_SESSION['verify']) {
echo '输入正确';
} else {
echo '验证码错误';
}
exit();
}
?>
<!DOCTYPE html>
<html>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<head>
<title>验证码验证</title>
</head>
<body>
<form action="form.php" method="post">
我是验证码:<img id="verify" src="./image.php?r=<?php echo rand(); ?>"><br/>
请输入验证码:<input type="text" name="verify"><br/>
<a href="javascript:void(0)" onclick="document.getElementById('verify').src='./image.php?r='+Math.random()">看不清楚,换一个</a>
<input type="submit" value="提交">
</form>
</body>
</html>
下面开始赘述以上出现的的PHP函数:
session_start();开启session 必须放在文档头部;
imagecreatetruecolor — 新建一个真彩色图像
用法:imagecreatetruecolor ( int
width,int
height );默认是黑色的;
imagecolorallocate — 为一幅图像分配颜色
用法:imagecolorallocate ( resource
image,int
red , int
green,int
blue ) 后面是RGB色彩;
imagefill — 区域填充
用法:imagefill ( resource
image,int
x , int
y,int
color )
imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
imagecolorallocate — 为一幅图像分配颜色
imagecolorallocate ( resource
image,int
red , int
green,int
blue ) 后面也是RGB
imagestring — 水平地画一行字符串
imagesetpixel — 画一个单一像素
imageline — 画一条线段
imagepng — 以 PNG 格式将图像输出到浏览器或文件
imagedestroy — 销毁一图像
下面开始贴出中文验证码的制作流程:
这里需要的函数有:
imagettftext — 用 TrueType 字体向图像写入文本
imagettftext ( resource
image,float
size , float
angle,int
x , int
y,int
color , string
fontfile,string
text )
其中size 为字体的尺寸;angle 表示角度,X,Y表示坐标,color 颜色,fontfile 要想使用字体的路径,text 要写入的文本。
下面直接贴代码:
<?php
//phpinfo();
header("content-type:text/html;charset=utf-8");
session_start();
$image=imagecreatetruecolor(200,60);
$bgcolor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgcolor);
//中文验证码
$check_verify = '';
for ($i=0; $i <4 ; $i++) {
$fontsize = 6;
$fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120), rand(0,120));
$font = 'simsun.ttc';
$data = '开发实例大全字符不够还可以加';
$num = mb_strlen($data,'utf8');
$fontcontent = mb_substr($data,rand(0,$num-1),1,'utf8');
$check_verify .= $fontcontent;
imagettftext($image,mt_rand(20,24),mt_rand(-60,60),(40*$i+20),mt_rand(30,35),$fontcolor,$font,$fontcontent);
}
$_SESSION['verify']=$check_verify;
for($i=0;$i<200;$i++){
$pointcolor = imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200));
imagesetpixel($image, rand(1,199), rand(1,59), $pointcolor);
}
for($i=0;$i<3;$i++){
$linecolor = imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220));
imageline($image,rand(1,199),rand(1,59),rand(1,99),rand(1,59),$linecolor);
}
header("content-type:image/png");
imagepng($image);
imagedestroy($image);
前台代码表示不变。
这里还有一点知识点:关于中文字符串的截取。
用到了mb_substr();
mb_substr ( string
str,int
start [, int
length=NULL[,string
encoding = mb_internal_encoding() ]] ) encoding为编码;
mb_strlen();
mb_strlen — 获取字符串的长度.