为了账号安全,请及时绑定邮箱和手机立即绑定

代码细节问题,自己做笔记写的,有点问题。

captcha.php代码如下:

session_start();
$image = imagecreatetruecolor( 100, 30 );// 新建一个真彩色图像

/**
* imagecolorallocate — 为一幅图像分配颜色。
* 说明:int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
*/
$bgcolor = imagecolorallocate($image,255,255,255);

/**
* imagefill — 区域填充。
* 说明:bool imagefill ( resource $image , int $x , int $y , int $color )
* 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
*/
imagefill( $image, 0, 0, $bgcolor );

/**
* 数字验证码的实现:主要用到 imagestring 这个函数;$i表示验证码个数
*/
//for($i=0; $i<4; $i++){
//    $fontsize = 5;
//    $fontcolor = imagecolorallocate( $image, rand(0,125), rand(0,125), rand(0,125) );
//    $fontcontent = rand(0,9);
//    $fontx = ($i*100/4) + rand(5,10);
//    $fonty = rand(5,10);
//
//    /**
//     *  bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
//     *  用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。
//     *  如果 font 是 1,2,3,4 或 5,则使用内置字体
//     */
//    imagestring($image,$fontsize,$fontx,$fonty,$fontcontent,$fontcolor);
//}

/**
* 字母验证码的实现:主要用到 imagestring 这个函数;$i表示验证码个数
*/
$captcha_code = '';

for($i=0;$i<4;$i++){
   $fontsize = 6;
   $fontcolor = imagecolorallocate( $image, rand(0,125), rand(0,125), rand(0,125) );
   $date = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789';
   $fontcontent = substr($date,rand(0,strlen($date)-1),1);

   $captcha_code .= $fontcontent;

   $fontx = ($i*100/4) + rand(5,10);
   $fonty = rand(5,10);

   /**
    *  bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
    *  用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。
    *  如果 font 是 1,2,3,4 或 5,则使用内置字体
    */
   imagestring($image,$fontsize,$fontx,$fonty,$fontcontent,$fontcolor);
}
$_SESSION['authcode'] = $captcha_code;

/**
* 验证码点干扰元素的实现:主要用到 imagesetpixel 函数;$i 表示干扰元素个数
*/
for($i=0;$i<200;$i++){
   $pointcolor = imagecolorallocate( $image, rand(50,200), rand(50,200), rand(50,200) );
   /**
    * bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
    * 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。
    * 如果 font 是 1,2,3,4 或 5,则使用内置字体。
    */
   imagesetpixel( $image,rand(1,99),rand(1,99),$pointcolor);
}

/**
* 验证码线干扰元素的实现:主要用到 imageline 函数:$i表示线的条数。
*/
for($i=0;$i<4;$i++){
   $linecolor = imagecolorallocate( $image, rand(80,220), rand(80,220), rand(80,220) );
   /**
    *  bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
    *  用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
    */
   imageline( $image, rand(1,99), rand(1,29), rand(1,99), rand(1,29), $linecolor);
}

header('content-type:image/png');

imagepng( $image );//imagepng — 以 PNG 格式将图像输出到浏览器或文件

imagedestroy( $image );//imagedestroy — 销毁一图像



from.php代码如下:

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/3/18
* Time: 9:34
*/
header("Content-type:text/html;charset=utf-8");
if(isset($_REQUEST["authcode"])){
   print_r($_REQUEST["authcode"]);
session_start();
print_r($_SESSION["authcode"]);
if( strtolower($_REQUEST["authcode"]) == $_SESSION["authcode"] ){
echo "1";
}else{
echo "0";
}
//    if ($_REQUEST["authcode"] == $_SESSION["authcode"])
//    {
//        header("Content-type: text/html; charset=UTF8");
//        echo "<h5 color="#0000CC">输入正确</h5>";
//    }else{
//        header("Content-type: text/html; charset=UTF8");
//        echo "<h5 color="#CC0000">输入错误</h5>";
//        echo $_REQUEST["authcode"];
//    }
exit;
}
?>

<!doctype html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>确认验证码</title>
</head>

<body>
<form method="post" action="./from.php">
   <p>验证码图片:<img border="1" src="./captcha.php/r=<?php echo rand();?>" width=100,height=30";</p>
   <p>请输入图片中的内容:<input type="text" name="authcode" value=""></p>
   <p><input type="submit" value="提交" style="padding:6px 20px;"></p>
</form>
</body>
</html>



具体不清楚哪儿有问题,验证信息的时候,两个数据都是一样的, 可是输出去却总是“0”,当局者迷,旁观者清。求大神帮忙看下~谢谢!

正在回答

1 回答

晓得是什么原因了~

0 回复 有任何疑惑可以回复我~
#1

Momo_____

可以告诉我为什么吗?我的代码也出现了这种问题QWQ
2017-06-07 回复 有任何疑惑可以回复我~
#2

Only_L 提问者 回复 Momo_____

还没解决吗?要是没解决,我下班回家的时候把代码拿出来给你看看。这很久之前的了,我都不知道哪出错了,回头看看程序笔记。
2017-06-13 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

代码细节问题,自己做笔记写的,有点问题。

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信