Create a simple Captcha in php
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
contact.php
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>
Create a simple Captcha in php
Reviewed by JS Pixels
on
April 07, 2012
Rating:
hi your topic is so useful this post must easy to understand if you can show us demo or something working
ReplyDeleteHi... Your code works very easily. I am trying to add it to my php that is running query's to my MySQL db, but it doesn't send the information to the db. If you have time to help me, may I copy and paste the scripts for your review? Thank you in advance... alex
ReplyDeleteYeah Sure.
Delete