Make Your own Captcha - Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Webmaster Tech > Programming > PHP Development

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-14-2010, 05:31 AM   #1 (permalink)
Regular User
 
Tharaka Deshan's Avatar
 
Join Date: Jan 2010
Location: Galle, Sri Lanka
Posts: 15
Default Make Your own Captcha

You may see hundreds of captchas in various sites. Captchas use to protect from hacking programs and robots. Simply to identify the user is a human.

Working of a captcha as below.
1. Generate a random text
2. Text written to an image
3. Text stored in session
4. Image displays to the user
5. User enters the code
6. User entered code is checked with the stored text
7. If they match then continue

Let’s make your own simple captcha now. In here we will make a captcha which has 5 numbers. So that we use rand() function in php.
For example rand(5, 15)will generate random number between 5 and 15. In our case we need a 5 digit number. So we can use this function as rand(10000, 99999).

Now we have the string and next step is to write it an image. So that we’re going to use my favorite GD library of image functions in PHP. Imagecreatefrompng() function is use to create a image start from a existing png image. In here we already have the “captcha.png” and the text will draw on that image.

$captcha = imagecreatefrompng("captcha.png")

Colors are allocate as follows in RGB,

$black = imagecolorallocate($captcha, 0, 0, 0)

Finally we have to write the text to the image which is made easy with imagestring()

imagestring($captcha, 5, 20, 10, $string, $black)

To view the image

<img xsrc="captcha.php" border="0">

Next thing is to store the text in session.

$_SESSION[‘text’] = $string

Finally to compare the correct code with the user given text, use following code

<?php
session_start();
If($_POST['text'])= $_SESSION['text'])
{
echo 'You entered the code correctly';
}else{
echo 'You entered the code wrong';
}
?>


The complete code of our simple captcha as follow.

File name:Index.html

<html>
<body>
<form action="compare.php" method="post">
<p>Enter the code below</p>
<img src="captcha.php" border="0"><br>
<input type="text" name="text" />
<p><input type="submit" value="Submit"/></p>
</form>
</body>
</html>


File name: captcha.php

<?php
header("Content-type: image/png");
session_start();
$string=rand(10000, 99999);
$captcha = imagecreatefrompng("captcha.png");
$black = imagecolorallocate($captcha, 0, 0, 0);
imagestring($captcha, 5, 25, 5, $string, $black);
imagepng($captcha);
$_SESSION['text'] = $string;
?>


File name: compare.php

<?php
session_start();
If($_POST['text']== $_SESSION['text'])
{
echo 'You entered the code correctly';
}else{
echo 'You entered the code wrong';
}
?>

Last edited by Tharaka Deshan; 01-14-2010 at 05:35 AM.
Tharaka Deshan is offline   Reply With Quote
Sponsored Links
Old 01-14-2010, 11:43 AM   #2 (permalink)
Junior Member
 
Join Date: Jan 2010
Posts: 9
Default Re: Make Your own Captcha

Hi Tharaka Deshan!
I have a code php form.
Code:
<form>
     <table>
      <tr>
        <td>Name *</td>
        <td><input type="text"></td>
      </tr>
      <tr>
          <td>Email *</td>
          <td><input type="text"></td>
      </tr>
      <tr>
          <td>Phone</td>
          <td><input type="text"></td>
      </tr>
      <tr>
          <td>Field_check</td>
          <td>
             <input type="checkbox" value="1"><label>Check_1</label><br>

             <input type="checkbox" value="2"><label>Check_2</label><br>
             <input type="checkbox" value="3"><label>Check_3</label>
          </td>
      </tr>
      <tr>
          <td>Message *</td>
          <td><textarea rows="5"></textarea></td>
      </tr>
          <tr>
          <td colspan="2"> </td>
      </tr>
          <tr>
          <td colspan="2"> </td>
      </tr>
      <tr>
          <td><input type="button" id="subbut" value="Submit"></td>
      </tr>
     </table>
</form>
Whether I should insert CAPCHA into this code, or use it separately?
faster is offline   Reply With Quote
Old 01-17-2010, 01:55 AM   #3 (permalink)
Regular User
 
Tharaka Deshan's Avatar
 
Join Date: Jan 2010
Location: Galle, Sri Lanka
Posts: 15
Thumbs up Re: Make Your own Captcha

Thank you very much for your reply.
The answer is yes you can do it.
Please insert the following codes to your html codes.

<img src="captcha.php" border="0">
<input type="text" name="text" />


But to check the given number, you have to add my codes in "compare.php" to your desired form action php file.
(But in your code you didn't place a action php.So after submit your form it will do nothing)
Pleace change the form tag as follows,


<form action="compare.php" method="post">


After change all, you will get a code like this,


<form action="compare.php" method="post">
<table>
<tr>
<td>Name *</td>
<td><input type="text"></td>
</tr>
<tr>
<td>Email *</td>
<td><input type="text"></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text"></td>
</tr>
<tr>
<td>Field_check</td>
<td>
<input type="checkbox" value="1"><label>Check_1</label><br>

<input type="checkbox" value="2"><label>Check_2</label><br>
<input type="checkbox" value="3"><label>Check_3</label>
</td>
</tr>
<tr>
<td>Message *</td>
<td><textarea rows="5"></textarea></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<tr><td><img src="captcha.php" border="0"></td>
<td><input type="text" name="text" /></td>
<tr> <td><input type="button" id="subbut" value="Submit"></td>
</tr>
</table>
</form>


Please reply me the progress.
Good luck!!
Tharaka Deshan is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Sponsors

» Links

» Affiliates
Web Hosting
Online Backup Reviews
Marketing Find
Merchant Select
SiteMap Builder
Host Compare

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 09:32 AM.


Powered by vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2
Webmaster Forums
Web Hosting | Chicago Web Hosting | Web Hosting