Friday, 9 September 2011

How to Make Your Own PHP Captcha Generator

3 Major Anti-spamming techniques used?
  1. Mathematical Operation like Random number + Random Number = -> The user must specify the answer
  2. Random word -> User must type the word
  3. Random question -> Obvious one which the user should answer correctly [ex: Are you human?]
How Captcha works?
  1. The captcha generator generates an IMAGE with the question and then put up a session variable storing the value.
  2. User input though an input box.
  3. Using php POST, we compare the session variable data with the user input and tell whether its a bot or human.

The Code

  1. First let's write the php script which generates the captcha image. We use the simple header-content change technique, from which we can easily bring up an image from a given text.
    captcha.php

    PHP Code:
    <?php //This should be the first 
    line as in the rule book :D session_start();
    //These variables store the 
    Question and the answer $ques ""; $ans ""
     //This is the MAJOR array, this holds 
    all the random things, 
    like the question you need to ask. 
    You can add up new ones easily $words = array(
            
    => array("Num" => "Num"),
            
    => array("Are you human?" => "yes"),
            
    => array("Type 'one' " => "one"),
            
    => array("Type 'test' " => "test"),
            
    => array("AxHGA" => "AxHGA"),
            
    => array("zontek" => "zontek"),
            
    => array("12terd " => "12terd")
        );
        
    //Now we need to pic up a random question, 
    array_rand is the perfect thing to this
        
    $r array_rand($words);
        
    //Then we check about what we have in the select array
        
    switch(key($words[$r])){
            
    //If we have the "NUM" selected, that is a 


    special one
            //Num means the user will be prompted to 

     do a simple addition like 5+6
            
    case "Num":
                
    //Pretty basic stuff, generate 2 random numbers and 
    tell the user to put the addition
                
    $i rand(1,10);
                
    $j rand(1,10);
                
    $ans $i+$j;
                
    $ques "$i + $j = ";
                break;
            default:
                
    //If not a number, ask the user a question or ask 
    him to type a word
                
    $key key($words[$r]);
                
    $ques $key;
                
    $ans $words[$r][$key];
                break;
        }
    //NOW we put up the answer to the session variable  

    $_SESSION['cap'] = strtolower($ans); 
     //This would change the content type, or in english 
    this would tell the browser that
    //what ever retuened by this script is an image 

     header('Content-Type: image/png');
    //Following code is to generate the image from the test
    //We first specify colour ranges, you can refer to 

    the php manaul for more $img imagecreatetruecolor(250,30);
    //In the above code, the image size is set to 250x30 
     $white imagecolorallocate($img,255,255,255); 
     $grey imagecolorallocate($img,128,128,128);
    $black imagecolorallocate($img,0,0,0);
    //Filling the rectangle with white as we need black text on white
    imagefilledrectangle($img,0,0,399,29$white); $text $ques;  

    //THE below code is CRITICAL. This is the palce 
    where we tell which font to use.
    //Choose any ttf you like and name it as font.ttf or 
    change the following code, make sue
    //you put the path to the file correctly [i used STENCIL 
    so that parsers will find it hard to detect]
    $font "./font.ttf";
    imagettftext($img,20,0,11,21,$grey,$font,$text);
    imagettftext($img,20,0,10,20,$black,$font,$text);
    //Creating a PNG image, i use png cuz i <3 png 
    [really its so small and efficient ;)]
    imagepng($img);
    //And then remove the memory parts once the output is given
    imagedestroy($img); ?>   
    index.php
    <?php
    session_start();
    if(isset($_POST['done'])){
        if(isset($_SESSION['cap'])){
            //This is the code for comparing the user input 
    against the session variable
            $cap = $_POST['captcha'];
            if($_SESSION['cap']==strtolower($cap)) echo "Okay you are human :)";
            else echo "Off You go BOT / SPAMMER!!";
        }
    } ?> <html>
    <head>
    <title>Simple Captcha Script</title>
    </head>
    <body>
    <form action="index.php" method="post">
    <img src="captcha.php" align="absmiddle" />
    <!-- NOTE how the captcha.php is used as an image link,

     that's a whole new way to think -->
    <input type="text" size="20" name="captcha" /><br />
    <input type="submit" name="done" value="Login" />
    </form>
    </body>
    </html>
    Upload the stuff and make sure you have the following files in the same directory level : index.php captcha.php font.ttf
     

No comments:

Post a Comment