Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numeric Captcha for PHP

Is there a numeric captcha available for PHP?

(which doesn't rely on JavaScript being turned on)


EDIT:

  • I know there are JS-independent captchas out there.
  • I know there are PHP captchas out there.
  • I know there are numeric captchas out there.

But I'm looking for a PHP numeric Javascript-independent captcha.
The only numeric captcha's I've found are either for ASP.NET or jQuery/JS based ones.
I don't want any of those as an answer to the question.

And I'm not taking about a small website here. In the answer I'd like to know whether your suggestion puts a lot of strain on the server or not.

like image 820
Adam Lynch Avatar asked May 18 '11 08:05

Adam Lynch


People also ask

How can I use CAPTCHA code in PHP?

// the captcha externally! imagestring( $im , rand(1, 7), rand(1, 7), rand(1, 7), $captcha , $fg );

How do I enter numeric CAPTCHA?

A CAPTCHA test is made up of two simple parts: a randomly generated sequence of letters and/or numbers that appear as a distorted image, and a text box. To pass a the test and prove your human identity, simply type the characters you see in the image into the text box.

How do I generate a CAPTCHA code?

To generate a unique CAPTCHA every time, a random number is generated using rand() function (rand()%62) which generates a random number between 0 to 61 and the generated random number is taken as index to the character array chrs[] thus generates a new character of captcha[] and this loop runs n (length of CAPTCHA) ...


2 Answers

I guess it can't be avoided to deal with rendering an image when dealing with captchas? Here's a simple one (and may not be the most elegant):

sample output

session_start();

$strings = '123456789';
$i = 0;
$characters = 6;
$code = '';
while ($i < $characters)
{ 
    $code .= substr($strings, mt_rand(0, strlen($strings)-1), 1);
    $i++;
} 

$_SESSION['captcha'] = $code;

//generate image
$im = imagecreatetruecolor(124, 40);
$foreground = imagecolorallocate($im, 0, 0, 0);
$shadow = imagecolorallocate($im, 173, 172, 168);
$background = imagecolorallocate($im, 255, 255, 255);

imagefilledrectangle($im, 0, 0, 200, 200, $background);

// use your own font!
$font = 'monofont.ttf';

//draw text:
imagettftext($im, 35, 0, 9, 28, $shadow, $font, $code);
imagettftext($im, 35, 0, 2, 32, $foreground, $font, $code);     

// prevent client side  caching
header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

//send image to browser
header ("Content-type: image/png");
imagepng($im);
imagedestroy($im);

Display it in a form:

<img src="captcha.php">
Enter the code above: <input type="text" name="captcha">

Once submitted, check the entered code:

if ($_POST['captcha'] == $_SESSION['captcha'])
    // do your thing
like image 158
tradyblix Avatar answered Nov 04 '22 15:11

tradyblix


CAPTCHA's dont necessarily rely on javascript (I think your thinking of reCATCHA), a stand alone image is all you need.

http://www.phpcaptcha.org/

At it's most simple, a numeric CAPTCHA would work like...

<?php
session_start();

if (isset($_POST['code'])) {
    if ($_POST['code'] == $_SESSION['captcha']) {
        echo "Captcha valid";
    }
    else {
        echo "Captcha NOT valid";
    }
}

$_SESSION['captcha'] = mt_rand(10000, 99999);
?>
<form action="" method="post">
    <p>Enter this number: <?php echo $_SESSION['captcha']; ?></p>
    <p><input type="text" name="code" /> <input type="submit" value="Submit" />
</form>
like image 21
fire Avatar answered Nov 04 '22 13:11

fire