Home » » Create a simple Captcha in php

Create a simple Captcha in php

Written By Unknown on Sabtu, 07 April 2012 | 00.42

If you know how to create images in php. Its very easy for you to create captcha. Just create an image and write some text on it. Your captcha image is ready.

You can use an existing image for your captcha background. Just replace the following line
$Im = imagecreate(80,22);
with
$Im = imagecreatefromjpeg('yourImage.jpg');
'yourImage.jpg' is the path of your image.

The full captcha script is given here. Just call the captcha.php file in the form where you want it to appear and check its value when the form is submitted.

captcha.php

  <?php
session_start();
$str = rand(11111,99999);
$_SESSION['captcha'] = $str;
$Im = imagecreate(80,22);
$imgcolor = imagecolorallocate($Im, 0, 0, 0);
$txtcolor = imagecolorallocate($Im, 255, 255, 255);
imagestring($Im, 5, 15, 3, $str, $txtcolor);
header("Content-type: image/jpeg");
imagejpeg($Im);
?>


contact.php

  <?php
session_start();
if (isset($_REQUEST['submit'])) {
if ($_POST['captcha'] == $_SESSION['captcha']) {
echo 'Captcha is correct. Do something here !';
} else {
echo 'Captcha is wrong. Take action !';
}
}
?>

<form action="contact.php" method="post">
<table border="0" width="100%">
<tr>
<td width="10%">Name </td>
<td width="90%">
<input type="text" name="name" /></td>
</tr>
<tr>
<td>Message </td>
<td><textarea name="message" ></textarea></td>
</tr>
<tr>
<td>Enter Captcha </td>
<td>
<input type="text" name="captcha" maxlength="5" size="9"/>
<img src="captcha.php" style="vertical-align:bottom;"/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>

Share this article :

Posting Komentar

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kumpulan Kata Broadcast Blackberry - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger