Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 验证码验证不需要';行不通_Php_Captcha - Fatal编程技术网

Php 验证码验证不需要';行不通

Php 验证码验证不需要';行不通,php,captcha,Php,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

我创建了一个验证码,但由于某些原因,我无法设置会话变量。 这里是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 ();

“我无法将$code设置为$\u会话”这是什么意思?为什么你不能?因为
imagejpeg
开始输出?@smuf当我执行echo$\u会话['test']时,它不会显示任何内容,正如@u\u mulder所说,尝试将
$\u会话['captcha']=$code
紧接着
session\u start()
此外,在脚本开头启动会话,以定义当前代码中的会话索引
captcha
。\u会话['captcha']
可能返回无效索引错误。
<img alt="" id="captcha" src="capture.php" />
            <span onclick="document.getElementById('captcha').src=document.getElementById( 'captcha').src + '?' + Math.random();">refresh captcha</span>