Re: PHP Turing test problem (readable)
| From: | Chris Hayes | Date: | Tue, 13 May 2003 13:02:36 +0000 |
| Subject: | Re: PHP Turing test problem (readable) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-147426@lists.php.net to get a copy of this message | ||
cause his code lost the returns somewhere, i added them again:
Turing Test
Are you human?
<?php
// Generate a Turing Test image and output it to the browser
function GenerateImage($token)
{$iFont = 5; // Font ID
$iSpacing = 2; // Spacing between characters
$iDisplacement = 4;// 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, 200, 200, 200);
$col2 = ImageColorAllocate ($pic, 0, 0, 100);
$col3 = ImageColorAllocate ($pic, 123, 245, 250);
$iX=1;
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 < 5; $i++) ImageLine ($pic, rand (0, $iWidth / 2), rand (0, $iHeight / 2), rand ($iWidth / 2, $iWidth),rand ($iHeight / 2, $iHeight), $col3);
ob_start();
ImageJPEG($pic);
$data = ob_get_contents();
ob_clean();
ImageDestroy($pic);
return $data;
}
function AdministerTest ($error = false)
{ // Generate a six-digit random string
$token = (string) rand (100000, 999999);
// Output form to the user
if ($error) echo 'ERROR! You\'re not human!';
?>
Please enter the combination you see in the string above:
<?
}
function CheckResults()
{ $token = $_POST['inp'];
$hash = $_POST['tok'];
if (md5 ($token . 'You\'ll never break this!') === $hash)
{ ?> 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();
?>