Custom PHP CAPTCHA generator

  • Hey guys, I wrote my own custom CAPTCHA image generator using PHP & the GD library and wanted to share it with you, in case you need something like this on your own sites or apps. It's a really simple implementation which you can see an example output here: 10b.us/captchar.php

    This PHP script generates a 150x80 PNG image consisting of a few random geometric shapes along with a CAPTCHA text string of 5 characters consisting of the numbers 0-9 and letters a-f. If you needed more characters or different colors, you could easily extend this script. The correct CAPTCHA value is stored in a session variable named "captchaval". The font I'm using is called "aescrawl.ttf" which has a handwriting feel to it. Substitute whatever font you'd like but it will need to be in the same directory as this script.

    Implementation: Think of this file as just any other image that you'd insert into your HTML as an img src. You would include this script inside that tag like this:

    <img src="captchar.php" alt="CAPTCHA image" />

    Here's the CAPTCHA PHP code itself:

    <?php
    
    /********************************************************
     *
     *    CAPTCHA generator v0.01, 2013
     *    Written by ionux (help@10b.us)
     *
     *    This is free software released into the 
     *    public domain. No warranty is implied or provided.
     *
     ********************************************************/
    
    ob_start();
    session_start();
    
    $string = substr(md5(mt_rand(0,55)),5,5);
    $spook = ':::::::::::';
    $shape1_x = rand(0,145);
    $shape1_y = rand(0,75);
    $shape2_x = rand(0,145);
    $shape2_y = rand(0,75);
    
    $_SESSION['captchaval'] = $string;
    
    header('Content-Type: image/png');
    
    $im = 

    imagecreate(150, 80) or die('Cannot Initialize new GD image stream');[/p] [/p] $background_color = imagecolorallocate($im, 0, 0, 0);[/p] $text_color = imagecolorallocate($im, 233, 233, 233);[/p] $strange_color = imagecolorallocate($im, 101, 101, 101);[/p] $shape1_color = imagecolorallocate($im, 191, 77, 224);[/p] $shape2_color = imagecolorallocate($im, 200, 111, 43);[/p] [/p] imagestring($im, 5, rand(3,100), rand(10,75), $spook, $strange_color);[/p] imagestring($im, 5, rand(3,100), rand(10,75), $spook, $strange_color);[/p] imageellipse($im, $shape1_x, $shape1_y, rand(46,100), rand(30,100), $shape1_color);[/p] imageellipse($im, $shape2_x, $shape2_y, rand(46,100), rand(30,100), $shape2_color);[/p] imagettftext($im, 34, rand(0,15), 10, 75, $text_color, 'aescrawl.ttf', $string);[/p] [/p] imagepng($im);[/p] imagedestroy($im);[/p]

    If this help anyone else, great! I appreciate all the help you guys have given me with C2 so far. :D

  • its possible to create captcha via events in C2?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • ionux

    EDIT: nvm I realised I scripted forwarding my session values to cookies for some debugging.

    @delgado

    ofc, you could easily add some effect to distort a text.

    The idea is that image recognition should fail to recognise the captcha characters. As long as potential abusers cant find the value in the source code or directly by some recognition tool, then your set.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)