• 0

[PHP] Turing Test


Question

I'm trying to work on a Turing Test (Human Check) on a registration page (just like on Neowin) and I looked around the net for some sample code. This is the only one I found that worked with my version of GD (version 1.6.2).

However, this code works on FireFox but not when using IE. Can anyone advise or perhaps post some better code?

Thanks.

<?php
// Generate a Turing Test image and output it to the browser

function GenerateImage($token){
 $iFont = 5; // Font ID
 $iSpacing = 5; // Spacing between characters
 $iDisplacement = 5; // Vertical chracter displacement

 // Establish font metric and image size
 $iCharWidth = ImageFontWidth ($iFont);
 $iCharHeight = ImageFontHeight ($iFont);
 $iWidth = strlen($token) * ($iCharWidth + $iSpacing);
 $iHeight = $iCharHeight + 2 * $iDisplacement;

 // Create the image
 $pic = ImageCreate ($iWidth, $iHeight);

 // Allocate a background and foreground colour
 $col = ImageColorAllocate ($pic, 250, 250, 250);
 $col2 = ImageColorAllocate ($pic, 0, 0, 0);
 $col3 = ImageColorAllocate ($pic, 200, 200, 250);

 $iX=4;
 for ($i=0; $i < strlen ($token); $i++){
  ImageChar ($pic, $iFont, $iX, $iDisplacement - (rand (-$iDisplacement,  $iDisplacement)), $token[$i], $col2);
  $iX += $iCharWidth + $iSpacing;
 }

 // Draw some lines
 for ($i = 0; $i < 2; $i++)
 ImageLine ($pic,  rand(0,$iWidth/2), rand(0,$iHeight/2),  rand($iWidth/2,$iWidth), rand($iHeight/2,$iHeight),  $col3);
 ob_start();
 ImageJPEG($pic,'',60);


 $data = ob_get_contents();
 ob_clean();
 ImageDestroy($pic);
 return $data;
}

function AdministerTest ($error = false){
 // Generate a six-digit random string
 $token = (string) rand (1000, 9999);

 // Output form to the user
 if ($error)
  echo '<b>ERROR! You\'re not human!</b><p />';
?>
<img src="data:image/jpeg;base64,<?= base64_encode (GenerateImage ($token))?>">
<form action="<?= $_SERVER['PHP_SELF'] ?>" method=post>
 <input type="hidden" name="tok" value="<?= md5 ($token . 'You\'ll never break this!') ?>">
 Please enter the combination you see in the string above:
 <input type="text" name="inp">
 <input type="submit">
</form>

<?
}

function CheckResults(){
 $token = $_POST['inp'];
 $hash = $_POST['tok'];
 if (md5 ($token . 'You\'ll never break this!') === $hash){
 ?><b>Congratulations! You're either human or a really smart machine!<?
}

else
 AdministerTest (true);
}

// If the user has not POSTed the page, then
// administer the test. Otherwise, check the results.

if (strcmp ($_SERVER['REQUEST_METHOD'], 'POST')){
 AdministerTest();
}else{
 CheckResults();
}
?>

Link to comment
https://www.neowin.net/forum/topic/139791-php-turing-test/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

My guess is it's relating to this line:

<img src="data:image/jpeg;base64,<?= base64_encode (GenerateImage ($token))?>">

IE probably doesn't support embedded image data in HTML. My recommendation would be to split the image generation out into another script, if possible.

Link to comment
https://www.neowin.net/forum/topic/139791-php-turing-test/#findComment-1715277
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.