首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在重定向后更改代码以使Captcha正常工作

如何在重定向后更改代码以使Captcha正常工作
EN

Stack Overflow用户
提问于 2020-09-22 02:36:30
回答 1查看 41关注 0票数 0

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

验证码生成器:

代码语言:javascript
复制
 <?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);
  }
?>

显示验证码图片的html代码:

代码语言:javascript
复制
 <img id="imgCaptcha" src="Captcha.php" style="float: right;" />

htaccess重写以重定向:

代码语言:javascript
复制
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^ index.php [L]

如何更改代码以显示重定向后的验证码图片?

EN

回答 1

Stack Overflow用户

发布于 2020-09-22 06:25:50

我找到了问题所在:

代码语言:javascript
复制
  imagettftext($im, 24, 3, 2, 32, $text_color, "fonts/grade.ttf", $text);

找不到字体文件。我将相对路径替换为绝对路径:

代码语言:javascript
复制
$font= __DIR__.'/fonts/grade.ttf';
imagettftext($im, 24, 3, 2, 32, $text_color, $font, $text);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63998122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档