经测试代码如下: image.PHP
/**
* 简单图片验证码
*
* @param
* @author 编程之家 jb51.cc jb51.cc
**/
header("Content-type: image/png");
$string = "abcdefghijklmnopqrstuvwxyz0123456789";
for($i=0;$i<6;$i++){
$pos = rand(0,36);
$str .= $string{$pos};
}
$img_handle = ImageCreate (60,20) or die ("Cannot Create image");
//Image size (x,y)
$back_color = ImageColorAllocate($img_handle,255,255);
//Background color RBG
$txt_color = ImageColorAllocate($img_handle,0);
//Text Color RBG
ImageString($img_handle,31,5,$str,$txt_color);
Imagepng($img_handle);
session_start();
$_SESSION['img_number'] = $str;
/*** 代码来自编程之家 jb51.cc(jb51.cc) ***/
form.PHP
/**
* 简单图片验证码
*
* @param
* @author 编程之家 jb51.cc jb51.cc
**/
<form action="result.PHP" method="post">
<img alt="Random Number" src="image.PHP">
<input type="text" name="num"><br>
<input type="submit" name="submit" value="Check">
</form>
/*** 代码来自编程之家 jb51.cc(jb51.cc) ***/
result.PHP
/**
* 简单图片验证码
*
* @param
* @author 编程之家 jb51.cc jb51.cc
**/
session_start();
if($_SESSION['img_number'] != $_POST['num']){
echo'The number you entered doesn't match the image.<br>
<a href="form.PHP">Try Again</a><br>';
}else{
echo'The numbers Match!<br>
<a href="form.PHP">Try Again</a><br>';
}
/*** 代码来自编程之家 jb51.cc(jb51.cc) ***/
原文链接:https://www.f2er.com/php/529011.html