php里图片验证码的实现的大概思路是:生成随机验证码,把验证码写到session中,生成图片,把验证码写到图片上,输出图片。
<?php
session_start();
session_register("login_check_number");
//如果浏览器显示“图像XXX因其本身有错无法显示”,可尽量去掉文中空格
//先成生背景,再把生成的验证码放上去
$img_height=70;//先定义图片的长、宽
$img_width=25;
$authnum='';
//生产验证码字符
$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
$list=explode(",",$ychar);
for($i=0;$i<4;$i++){
}
//把验证码字符保存到session
$_SESSION["login_check_number"] = $authnum;
$aimg = imagecreate($img_height,$img_width);
imagecolorallocate($aimg, 255,255,255);
$black = imagecolorallocate($aimg, 0,0,0);
for ($i=1; $i<=100; $i++) {
}
//为了区别于背景,这里的颜色不超过200,上面的不小于200
for ($i=0;$i<strlen($authnum);$i++){
}
imagerectangle($aimg,0,0,$img_height-1,$img_width-1,$black);//画一个矩形
Header("Content-type: image/PNG");
ImagePNG($aimg);
ImageDestroy($aimg);
?>
生成的验证码存放在$_SESSION['login_check_number']中。
写一人简单的验证页面,如verify.html:
<form method='post' action='check.php'>
<input type='text' name='verify'>
<img style="cursor:pointer" title="刷新验证码" id="refresh" border='0' src='verify.php'
οnclick="document.getElementById('refresh').src='verify.php?t='+Math.random()"/>
<button type='submit'>确定</button>
</form>
在处理验证的页面得到输入验证码的input框的值和$_SESSION['login_check_number']中的值相比较,如果相等则输入验证码正确。
check.php
<?php
?>