0

i have created a captcha image with php code. the following is my code to create random captcha image. these codes have saved in a file that it's name is captcha.php

<?php
session_start();
header("Content/text:image/png");
$_SESSION["captcha"]=rand(10000,99999);
$captchaText=$_SESSION["captcha"];
//----- captcha properties variables -------
$captchaTextSize=30;
$captchaTextAngle=rand(5,15);
$X_ofCaptchaText=40;
$Y_ofCaptchaText=50;
$captchaFont="../../fonts/captchaFont.TTF";
$captchaImageWidth=190;
$captchaImageHeight=65;
//-------- noise properties variables --------
$captchaNoise="===================";
$captchaNoiseSize=15;
$captchaNoiseAngle=0;
$captchaNoiseColor=
$X_ofCaptchaNoise=0;
$Y_ofCaptchaNoise=rand(25,55);
//--------------------------------------------
$captchaImage=imagecreate($captchaImageWidth,$captchaImageHeight);
imagecolorallocate($captchaImage,255,255,255);//set the background color of an image
$captchaTextColor=imagecolorallocate($captchaImage,0,128,255); //set the font color 
imagefttext($captchaImage,$captchaTextSize,$captchaTextAngle,
    $X_ofCaptchaText,$Y_ofCaptchaText,$captchaTextColor,
    $captchaFont,$captchaText);
imagefttext($captchaImage,$captchaNoiseSize,$captchaNoiseAngle,
    $X_ofCaptchaNoise,$Y_ofCaptchaNoise,$captchaTextColor,$captchaFont,
    $captchaNoise); //set the noise on the image
imagepng($captchaImage);

and in my page i fetch captcha.php in an img tag as src:

<img src="model/captcha/captcha.php" id="captcha"/>

when the page load by http, there is no problem and captcha image loads successfuly:

enter image description here

but when i open the pages that contains the above img tag with https protocol, captcha doesnt load:

enter image description here

in addition when use https in page url this error appear in console: net::ERR_HTTP2_PROTOCOL_ERROR

i have tried it in google chrome and fire fox.both of them have this problem

hata.IR
  • 73
  • 5
  • https://stackoverflow.com/questions/58215104/whats-the-neterr-http2-protocol-error-about – CBroe Sep 21 '22 at 11:19
  • tnx. one of solutions of that post solved my problem. i will shared it's as current question answer. – hata.IR Sep 21 '22 at 15:30
  • 1
    Does this answer your question? [What's the net::ERR\_HTTP2\_PROTOCOL\_ERROR about?](https://stackoverflow.com/questions/58215104/whats-the-neterr-http2-protocol-error-about) – Martin Sep 21 '22 at 15:50

1 Answers1

1

For this issue Mr CBroe has suggested a older stackoverflow post that contains many solution for my problem that one of them solve my problem almost.

in my code at 3th line there is a header() function that cused Error, and not captcha image loading : header("Content/text:image/png");

So Chrome checks the accuracy of the data transmitted via the headers, and if it does not correspond, it fails.

I deleted header function and everything works fine now!!! but i think it may will take a problem at the future. beacuase header() function in my code make the file ready to contains the image (captcha image)

in that solution there is a reasone and a suggestion too:

"I found why content-length via filesize was being miscalculated: the GZIP compression is active on the PHP files, so excluding the file in question will fix the problem. Put this code in the .htaccess:

SetEnvIfNoCase Request_URI ^ / thumb.php no-gzip -vary

but i can not undrestand an dont have use it.

hata.IR
  • 73
  • 5
  • `Content/text` is simple not an allowed header name, nor the the correct one. It should be `Content-Type: image/png` – CBroe Sep 22 '22 at 06:09