Heres a simple php capcha script

Started on the old forum in August 2015 · view the original in the archive · old links redirect here

6 replies · 406 views · started 11 years ago · 1 min read

2 participants
Android404 Junior · 15 posts #1

In the form that you want to submit ad the following code

<?php
    $min_number = 1;
    $max_number = 10;

    $random_number1 = mt_rand($min_number, $max_number);
    $random_number2 = mt_rand($min_number, $max_number);
    echo "<span style=\"background-color:#000000; color:#FFFFFF; border:#000099 thin solid\"><b><big>$random_number1 + $random_number2</big></b></span><br />";

?>
<input name="result" type="text" size="3" /><br />
<input name="n1" type="hidden" value="<?=$random_number1; ?>" />
<input name="n2" type="hidden" value="<?=$random_number2; ?>" />

Ok, in the submit page ad the following code

<?php
    $result = $_POST["result"];
    $n1 = $_POST["n1"];
    $n2 = $_POST["n2"];
    $total = $n1 + $n2;

if ($result == $total) {
    // Paste content here, which will show if the code is entered correct. I left the javascript in just to make it easier for you to understand for first use.
echo "<script>alert('Correct answer');</script>";
} else {
    // If the code is wrong it will outpt this...
echo "<script>alert('Wrong answer entered, please retry');</script>";
    
}
?>

This is just an example, but it can be more modified, but this is a simple way of having a capcha system on your website. I will be posting another code too soon which uses images instead of text.

Android404 Junior · 15 posts #3

GumSlone wrote: bots or just simple grabbers will be able to read it

Lol you are right....

That's why i did this one. Insert or edit this to the form of the page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<div align="center">
<div align="center" style="color:#FF0000; border:#FF0000 thin solid; width:15%">
<form action="2.php" method="post">
<?php
    $random_number = rand('1','9999');    
    echo "<img src=\"img.php?text=$random_number\" alt=\"$random_number\"  /><br />";
?>
<b>Please enter the verification code as above</b><br />
<input name="result" type="text" size="6" /><br />
<input name="random_number" type="hidden" value="<?=$random_number; ?>" />
<input type="submit" value="Submit" />
</form>
</div>
</div>
</body>
</html>

that should go on the page where you $_POST

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
    $result = $_POST["result"];
    $random_number = $_POST["random_number"];

if ($result == $random_number) {
    // captcha is OK
echo "<script>alert('Correct answer');</script>";
} else {
    // wrong captcha
echo "<script>alert('Wrong answer entered, please retry');</script>";
    
}
?>
</body>
</html>

And this is my img.php file

<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(70, 30);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = $_GET['text'];
$font = 'georgia.ttf';
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);
?>

You think this will work better?

GumSlone Administrator Principal · 1,690 posts #4

still not safe since grabbers can read the text value from image tag,

you should create some own encoding function which will encode the text value for image tag, and decode it back to normal in the captcha check page.

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Android404 Junior · 15 posts #5

mmm i think i've got a way, but i will also use this little coding as well, because bots use source instead of html...

<form action="4.php" method="post">
<label>Test</label><br />
<input type="text" name="test" />
<div style="visibility: hidden">
<label>I'm a human</label>
<input type="checkbox" name="human" value="robit" />
</div>
<input type="submit" value="Submit" />
</form>

check page

<?php
$test=$_POST['test'];
if (empty($_POST['human']))
{
echo "You are a human";
}
else
{
echo "You are a bot";
}
?>

Suppose i can ad a few fields and try and trick the bots this way as well? It's a simple code, but any defence should be better than none?

GumSlone Administrator Principal · 1,690 posts #6

just create your own encoding and decoding function, its simple, i.e. replace 0 with a, b with 2 and convert it back on the captcha check page

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Android404 Junior · 15 posts #7

nice idea, thanks 🙂 i will do it in the morning and post back here.

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close