代码如下,怎么样才算成功呢,后续修改[PHP] 纯文本查看 复制代码 <?php
require "Captcha.class.php";
$captcha=new Captcha();
$captcha->generate(70,22,5);
?> [PHP] 纯文本查看 复制代码 <?php
class Captcha{
public function generate($img_w=100,$img_h=25,$char_len=4,$font=5){
$char =array_merge(range('A','Z'),range('a','z'),range(1,9));
$rand_keys=array_rand($char,$char_len);
if($char_len==1){
$rand_keys=array($rand_keys);
}
shuffle($rand_keys);
$code='';
foreach($rand_keys as $key){
$code .=$char[$key];
}
@session_start();
$_SESSION['captcha_code']=$code;
$img=imagecreatetruecolor($img_w,$img_h);
$bg_color=imagecolorallocate($img,0xc0,0xc0,0xc0);
imagefill($img,0,0,$bg_color);
for($i=0;$i<=300;++$i){
$color=imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($img,mt_rand(0,$img_w),mt_rand(0,$img_h),$color);
}
$rect_color=imagecolorallocate($img,0xff,0xff,0xff);
imagerectangle($img,0,0,$img_w-1,$img_h-1,$rect_color);
if(mt_rand(1,2)==1){
$str_color=imagecolorallocate($img,0,0,0);
}else{
$str_color=imagecolorallocate($img,0xff,0xff,0xff);
}
$font_w=imagefontwidth($font);
$font_h=imagefontheight($font);
$str_w=$font_w*$char_len;
imageString($img,$font,($img_w-$str_w)/2,($img_h-$font_h)/2,$code,$str_color);
header('Content-Type:image/png');
imagepng($img);
imagedestroy($img);
}
}
?> [PHP] 纯文本查看 复制代码 <?php
header("Content-Type:text/html;charset=utf-8");
session_start();
$code=trim($_POST["captcha"]);
if(strtolower($code)==strtolower($_SESSION['captcha_code'])){
echo "验证码正确";
$username=$_POST["username"];
$password=$_POST["password"];
if(($username=='itcast') && ($password=='123456')){
echo '你好'.$username.'登录成功';
}else{
echo '用户名或密码错误!';
}
}else{
echo "验证码输入错误";
}
?> |