PHP Captcha Script | Lessoncup Programming Blog

Tuesday, 20 May 2014

PHP Captcha Script

Hi, today i am going to tell you about how to place a captcha script in your form using php, the captcha is a very basic and regular tool were developers place it in submitting forms, the reason to place a captcha script is avoid spam to check weather if the person is live human or computer program is attempting to spam your website, i am just showing you how to generate a captcha code format using php


PHP

in php file take a img tag and assign a captcha.php file to src attribute
<table width="75%" border="0" align="center" cellpadding="0" cellspacing="0" >
  <tr>
    <td height="50" align="center" valign="middle" bgcolor="#666666" class="head">Captcha Script</td>
    </tr>

  <tr>
    <td height="120" align="center" valign="middle" class="chart">
      <img src="captcha.php" align="absmiddle"/>
       </td>
  </tr>
  </table>

captcha.php

here start the session for create the new session for every generating captcha code, now use encrypted function md5() and get the time stamp using microtime() function and insert into md5() function, now i am taking substr() function to get the captcha string, and i am assigning the generated captcha script to $_session['captcha'] , now i am putting a background image for generated captcha script by using imagecreatefromjped("captcha.jpg") function and converting our captcha script string using imagestring() to overlap our string on background image and adjusting the x and y positions.
<?php
session_start();

$random = md5(microtime());

$random = substr($random, 0, 8);

$_SESSION['captcha'] = $random;

$image = imagecreatefromjpeg("captcha.jpg");

imagestring($image, 6, 100, 40, $random,0);

header("Content-type: image/png");

imagepng($image);
?>

Live Demo   Download Script

3 comments :

  1. This script would only serve to annoy your visitors. Bots could easily break the captcha for the following reasons:

    * The captcha's x,y position is the same for each letter, every time it is generated (due to the font used, and no randomization of location) thus a bot could square-check each letter easily. A fix for this would be to randomize location of each letter, and using a different font (or even randomizing font!)

    * No random background image allows for easier bot-optimization, if you were to bot on a form that uses this captcha example. A fix would be to have a good amount of background images to choose from (like real captcha's do).

    * Background image is blurred, font is not. This results in it being extremely easy to invert/filter the image, and make the font stand out much more.

    Example/proof provided: http://i.imgur.com/jn2T4bG.png

    ReplyDelete
  2. Nice article Dear, I love to read this types of articles. Thanks a lot to share with us...... Searching for Govt jobs Click Here For Latest Govt Jobs.

    ReplyDelete
  3. I like it, and how to use the values in ramdom to compare whith user imput?

    ReplyDelete

Mohammad Khasim
Blogger | Web Developer
Hyderabad - INDIA
lessoncup@gmail.com

PHP | My SQL | JQUERY
Web Development Tools | CMS | Frameworks | SEO

Get my Latest Lessons