0

I encounter PHP Captcha is not displaying text to certain web browser.

I tried it on my desktop using Firefox and IE, it was able to work. When I tried like other party desktop or mobile devices, it did not display the text in the captcha.

At first, I thought the reason was because the font does not exist inside cPanel and causing the font not to display. Therefore, I uploaded two different fonts and try.

  • arial.ttf
  • NotoSans.ttf

But the same problem occurred.

This is how my code and File Manager look like. Please let me know if you need further detail information.

session_start();
$captcha_answer = $_SESSION['captcha_answer'];
$handle = ImageCreate (100, 40) or die ("Cannot Create image"); 
$bg_color = ImageColorAllocate ($handle, 176, 26, 72); //green
$font_color = ImageColorAllocate ($handle, 255, 255, 255);
$font = 'NotoSans.ttf';

imagettftext($handle, 20, 0, 10, 20, $bg_color, $font, $captcha_answer);
imagettftext($handle, 20, 7, 13, 32, $font_color, $font, $captcha_answer);

ImagePng($handle);

cPanel File Manager - Click Here to View

Update Solution Found

It is because HTTP > HTTPS issue.

Aside from the technical aspects of how Transport Layer Security works, the other facet of securely exchanging data lies in how well we apply that security. For example, if we allow a user to submit an application’s login form data over HTTP we must then accept that an MitM is completely capable of intercepting that data and recording the user’s login data for future use. If we allow pages loaded over HTTPS to, in turn, load non-HTTPS resources then we must accept that a MitM has a vehicle with which to inject Cross-Site Scripting attacks to turn the user’s browser into a pre-programmed weapon that will operate over the browser’s HTTPS connection tranparently.

Source from: http://phpsecurity.readthedocs.org/en/latest/Transport-Layer-Security-(HTTPS-SSL-and-TLS).html

Community
  • 1
  • 1
Wee Hong
  • 585
  • 2
  • 8
  • 23
  • is there any value in $captcha_answer ?? echoing it and see is there any value in it or not. – Shailesh Katarmal Aug 26 '15 at 05:39
  • @Kane: Would you like to give more details about the solution? You mention the cause of the problem but you don't say anything about what you did to solve it. I'd like to know what you did just from curiosity but I guess this would be also useful for someone with the same problem in the future. – Master_ex Aug 26 '15 at 08:51
  • @Master_ex, Basically the captcha.php won't load if the link is http://www.example.com/ instead of https://www.example.com/. I found that http://[www.example.com/ might block it because of security purpose. I edited the answer. Please take a quick read. Correct me if I am wrong. – Wee Hong Aug 26 '15 at 09:10
  • @ShaileshKatarmal, Yes, I can echo the $captcha_answer. But it is not $captcha_answer so far, I think it is because the protocol issue. – Wee Hong Aug 26 '15 at 09:11

2 Answers2

0

Try setting the correct header for a png image:

session_start();
$captcha_answer = $_SESSION['captcha_answer'];
$handle = ImageCreate (100, 40) or die ("Cannot Create image"); 
$bg_color = ImageColorAllocate ($handle, 176, 26, 72); //green
$font_color = ImageColorAllocate ($handle, 255, 255, 255);
$font = 'NotoSans.ttf';

imagettftext($handle, 20, 0, 10, 20, $bg_color, $font, $captcha_answer);
imagettftext($handle, 20, 7, 13, 32, $font_color, $font, $captcha_answer);

header('Content-Type: image/png');
ImagePng($handle); 

Furthermore, I suggest to set the headers so the browser not caching the image, in case the image is always dynamically generated. See this answer about how to achieve that.

Community
  • 1
  • 1
Master_ex
  • 789
  • 6
  • 12
0
@session_start();
$_SESSION['captcha_answer'] = "hello";
$captcha_answer = $_SESSION['captcha_answer'];
$handle = ImageCreate (100, 40) or die ("Cannot Create image"); 
$bg_color = ImageColorAllocate ($handle, 176, 26, 72); //green
$font_color = ImageColorAllocate ($handle, 255, 255, 255);
$font = 'font/monofont.ttf';  // your font path goes here....

header('Content-Type: image/png');
imagettftext($handle, 20, 0, 10, 20, $bg_color, $font, $captcha_answer);
imagettftext($handle, 20, 7, 13, 32, $font_color, $font, $captcha_answer);

ImagePng($handle);
Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Shailesh Katarmal
  • 2,757
  • 1
  • 12
  • 15