第一步创建图像框的php文件即,che.php
<?php
session_start();
for($i=0;$i<3;$i++){
$rand.= dechex(rand(1,50));
}
$_SESSION[check_pic]=$rand;
$im=imagecreatetruecolor(100,30);
//设置颜色
$bg=imagecolorallocate($im,0,0,0);//第一次调用调色板时的背景颜色,黑色
$te=imagecolorallocate($im,255,255,255);//第二次调用面板时的背景颜色,白色
//把字符串写在图像的左上角
imagestring($im,rand(1,6),rand(3,50),rand(3,16),$rand,$te);//imagestring(画板,字体,X,Y,内容,颜色);
//输出图像,不输入就不得行三,不知是啥子道理哟。
header("Content-type: image/jpeg");
imagejpeg($im);
?>
其次,创建sub.php调用以上php文件实现验证:
<?php
session_start();
if($_POST[check]){
if($_POST[check]==$_SESSION[check_pic]){
echo "密码正确".$_SESSION[check_pic];
}else
{
echo "密码错误哈".$_SESSION[check_pic];
}
}
?>
<form action="sub.php" method="post" >
<img src="che.php"/><br>
<input type="text" name="check" value="" /><br/>
<input type="submit" name="submit" value="确认提交"/>
</form>
结果如下图:
