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


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" >
    <td height="50" align="center" valign="middle" bgcolor="#666666" class="head">Captcha Script</td>

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


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.

$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");


Live Demo   Download Script


  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:

  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.

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

  4. Thanks friends, for providing such enlightening data.
    UX design company

  5. Obviously, once in a while individuals had issues seeing the picture accurately. cheap website hosting


Mohammad Khasim
Blogger | Web Developer
Hyderabad - INDIA

Web Development Tools | CMS | Frameworks | SEO

Get my Latest Lessons