验证码验证不起作用

时间:2018-05-12 17:17:06

标签: php captcha

我创建了一个Captcha,但由于某种原因我无法设置会话变量。 这是captcha.php

<?php

$captcha_length = 6;
$image = imagecreatetruecolor ( 200, 75 );
$back_color = imagecolorallocate ( $image, 255, 255, 255 );
$code = "";

for($i = 0; $i < $captcha_length; $i ++) {

    $x_axis = 20 + ($i * 20);
    $y_axis = 50 + rand ( 0, 7 );

    $color1 = rand ( 001, 150 );
    $color2 = rand ( 001, 150 );
    $color3 = rand ( 001, 150 );
    $txt_color [$i] = imagecolorallocate ( $image, $color1, $color2, $color3 );

    $size = rand ( 20, 30 );

    $angle = rand ( -15, 15 );

    $sign = function ($len=1) {
        $s = "";
        $b="qwertyuopasdfghjkzxcvbnm123456789";
        while($len-->0)  {
            $s.=$b[mt_rand(0,strlen($b))];
        }
        return "$s";
    };
    $t = $sign();
    $code .= "$t";

    imagettftext ( $image, $size, $angle, $x_axis, $y_axis, $txt_color[$i], "C:\OSPanel\domains\kultprosvet\arial.ttf", $t);
}
session_start();

header ( "Cache-Control: no-cache" );

header ( "Content-type: image/jpg" );

imagejpeg ( $image);

$_SESSION['captcha'] = $code;


exit ();

以下是来自html表单的片段,其中显示了页面上的验证码

<img alt="" id="captcha" src="capture.php" />
            <span onclick="document.getElementById('captcha').src=document.getElementById( 'captcha').src + '?' + Math.random();">refresh captcha</span>

所以Captcha会创建,但我无法将$code设置为$_SESSION。出于这个原因,我无法验证捕获。

0 个答案:

没有答案