This PHP program demonstrates how to create a captcha form with the Graphics Draw, GD, library.
<!DOCTYPE html>
<html>
<head>
<title>XoaX.net PHP</title>
</head>
<body style="background-color:#F0F0E0;">
<?php
session_start();
$_SESSION['Time']=time();
// Check that the form text was submitted
if (isset($_POST["qInput"])) {
$sInput = $_POST["qInput"];
// Check that the correct text was entered
if (isset($_SESSION['CaptchaCode']) && $sInput == $_SESSION['CaptchaCode']) {
DisplayCorrect();
return;
} else {
DisplayIncorrect();
}
}
// Display the initial page
CreateCaptchaImageAndDisplayForm();
?>
</body>
</html>
<?php
function DisplayCorrect() {
?>
<div style="text-align:center;">
<h1>Your answer is correct!</h1>
<form action=" <?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="submit" value="Refresh the Page">
</form>
</div>
<?php
}
?>
<?php
function DisplayIncorrect() {
?>
<div style="text-align:center;">
<h1>Your answer is incorrect!<br>please try again </h1>
</div>
<?php
}
?>
<?php
function CreateCaptchaImageAndDisplayForm() {
$iImageWidth = 200;
$iImageHeight = 50;
$qCaptchaImage = ImageCreateTrueColor($iImageWidth, $iImageHeight);
$qBkgdColor = ImageColorAllocate($qCaptchaImage, 255, 255, 255);
// Draw the background color
ImageFilledRectangle($qCaptchaImage, 0, 0, $iImageWidth, $iImageHeight, $qBkgdColor);
// Draw lines to obscure the text
$qLineColor = ImageColorAllocate($qCaptchaImage, 64, 64, 64);
for ($i = 0; $i < 10; ++$i) {
ImageLine($qCaptchaImage, 0, rand()%50, 200, rand()%50, $qLineColor);
}
// Draw some random pixels to obscure the text
$qPixelColor = ImageColorAllocate($qCaptchaImage, 0, 0, 255);
for ($i = 0; $i < 1000; ++$i) {
ImageSetPixel($qCaptchaImage, rand()%200, rand()%50, $qPixelColor);
}
// Generate the code
$sChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$iCount = strlen($sChars);
$qCharColor = ImageColorAllocate($qCaptchaImage, 0, 0, 0);
$sCode = '';
for ($i = 0; $i < 6; ++$i) {
$sChar = $sChars[rand(0, $iCount - 1)];
ImageString($qCaptchaImage, 5, 25 + ($i * 30), 15 + (rand()%5), $sChar, $qCharColor);
$sCode .= $sChar;
}
$_SESSION['CaptchaCode'] = $sCode;
// Remove the old saved files.
$qaImageFiles = glob("*.png");
foreach($qaImageFiles as $qOldFile) {
unlink($qOldFile);
}
// Save the png image file
ImagePNG($qCaptchaImage, "captcha".$_SESSION['Time'].".png");
?>
<div style="text-align:center;border:1px black solid;padding:5px;background-color:#FFFFEE;">
<h3>Enter the text in this image to verify that you are human.</h3>
<b>Refresh the page if you can not read the letters.</b>
<div style="display:block;margin-bottom:20px;margin-top:20px;">
<img src="captcha<?php echo $_SESSION['Time']?>.png">
</div>
<form action=" <?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" name="qInput"/>
<input type="submit" value="Submit" name="qSubmit"/>
</form><br />
<form action=" <?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="submit" value="Refresh the Page">
</form>
</div>
<?php
}
?>
© 20072025 XoaX.net LLC. All rights reserved.