Captcha Protection
Published: 03/03/10
Creating a CAPTCHA with PHP :: PHP Tutorial
- Requirement(s): PHP Server
Code:
2.Imagecreate - Create a new palette based image.
Code:
3.The function imagecolorallocate creates a color using RGB (red,green,blue) format.
Code:
4.Displaying the random text on the captcha image
Code:
5.Check if the security code and the session value are not blank and if the input text matches the stored text.
Code:
index.php:
Code:
captchas.php:
Code:
cc.php:
Code:
Add a Comment
Creating a CAPTCHA with PHP :: PHP Tutorial
This tutorial will show you how to create a form with Captcha. 1.Sessions in PHP are started by using the session_start() function. Like the setcookie( ) function, the session_start( ) function must come before any HTML, including blank lines, on the page.Code:
<?php session_start(); ?>
Code:
$img = imagecreate(40, 20);
Code:
$black = imagecolorallocate($img, 0, 0, 0);
Code:
$numero = rand(100, 999);
Code:
if(($_POST['check']) == $_SESSION['check'])
{ echo 'Input OK';
}else{
echo 'Input Wrong';
}
Files
index.php:
Code:
<form method="POST" action="cc.php">
<img src="captchas.php"> <br>
<input type="text" size="10" name="check"> <br>
<input type="submit" name="submit" value="submit">
</form>
Code:
<?php
session_start();
//imagecreate -- Create a new palette based image
$img = imagecreate(40, 20);
//displaying the random text on the captcha image
$black = imagecolorallocate($img, 0, 0, 0);
$numero = rand(100, 999);
$number = $black . $numero;
$_SESSION['check'] = ($numero);
$white = imagecolorallocate($img, 255, 255, 255);
imagestring($img, 10, 8, 3, $numero, $white);
header ("Content-type: image/png");
imagepng($img);
?>
Code:
<?php
//Sessions in PHP are started by using the
//session_start() function.
//Like the setcookie( ) function,
//the session_start function must come before any HTML,
//including blank lines, on the page.session_start();
//Check if the security code and
//the session value are not blank
//and if the input text matches the stored text
if(($_POST['check']) == $_SESSION['check']) {
echo 'Input OK';
}else{
echo 'Input Wrong';
}
Comments
Add a Comment
Sitemap