简体   繁体   English

重定向后如何更改我的代码以使用验证码

[英]How to change my code to work Captcha after redirecting

I'm changing my website codes.我正在更改我的网站代码。 I'm using routing system then I'm redirecting all request to index.php file by.htaccess file.我正在使用路由系统,然后通过 .htaccess 文件将所有请求重定向到 index.php 文件。 now my captcha image doesn't work.现在我的验证码图像不起作用。

captcha generator:验证码生成器:

 <?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 code to show captcha image:显示验证码图像的 html 代码:

 <img id="imgCaptcha" src="Captcha.php" style="float: right;" />

htaccess rewrite to redirect: htaccess 重写重定向:

  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^ index.php [L]

how to change my code to show my captcha image after redirecting?如何更改我的代码以在重定向后显示我的验证码图像?

I found the issue:我发现了问题:

  imagettftext($im, 24, 3, 2, 32, $text_color, "fonts/grade.ttf", $text);

it could not find the font file.它找不到字体文件。 I replace the relative path with absolute path:我用绝对路径替换相对路径:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM