Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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,我正在更改我的网站代码。我使用路由系统,然后通过htaccess文件将所有请求重定向到index.php文件。现在我的验证码图像不起作用了 验证码生成器: <?php session_start(); //Send a generated image to the browser create_image(); exit(); header("Content-type: image/png"); function create_image()

我正在更改我的网站代码。我使用路由系统,然后通过htaccess文件将所有请求重定向到index.php文件。现在我的验证码图像不起作用了

验证码生成器:

 <?php
  session_start();
  //Send a generated image to the browser 
  create_image(); 
  exit();

 header("Content-type: image/png");
 function create_image(){
 $x = 98;
 $y = 45;
 $im = imagecreate($x,$y);
 $bgcolor=imagecolorallocate($im,13,17,25);


 $text_color = imagecolorallocate($im,255, 255, 210);

function rand_string( $length ) {
$chars = "abcdklmnopqrstuvwxyzABCDEF0123456789GHIJKefghijLMNPQRSTUVWXYZ0123456789"; 
$str ="";
$size = strlen( $chars );
for( $i = 0; $i < $length; $i++ ) {
    $str .= $chars[ rand( 0, $size - 1 ) ];
}

   return $str;
}

 $text=rand_string(rand(1,2));
 $_SESSION['security_code']=strtolower($text);

 imagettftext($im, 24, 3, 2, 32, $text_color, "fonts/grade.ttf", $text);
 $color = imagecolorallocate($im, 255,222, 255);


for($i =2; $i < 55; $i+=mt_rand(2,6)) {
  for($j =4; $j < 45; $j+=mt_rand(1,5)) {
  
    imagesetpixel($im, $i,$j, $color);
 }

  } 
        
  imagejpeg($im);
  imagedestroy($im);
  }
?>
重定向后如何更改代码以显示验证码图像?

我发现了问题:

  imagettftext($im, 24, 3, 2, 32, $text_color, "fonts/grade.ttf", $text);
它找不到字体文件。 我将相对路径替换为绝对路径:

$font= __DIR__.'/fonts/grade.ttf';
imagettftext($im, 24, 3, 2, 32, $text_color, $font, $text);

删除
exit()
并重试,代码在
exit()后停止执行不,这不是问题所在
  imagettftext($im, 24, 3, 2, 32, $text_color, "fonts/grade.ttf", $text);
$font= __DIR__.'/fonts/grade.ttf';
imagettftext($im, 24, 3, 2, 32, $text_color, $font, $text);