Sat. Feb 29th, 2020

Host your Website

Read and learn

PHP Captcha Tutorial

1 min read



Download phpDesigner7 today at http://mpsoftware.dk for a free 21 day trial!

Video tutorial explaining how to create a CAPTCHA system using PHP.

28 thoughts on “PHP Captcha Tutorial

  1. captcha.php
    ==================================================================
    <?php

    ob_start();

    session_start();

    if(!isset($_POST['submit'])){

    echo "<form method="post" action="captcha.php">n";

    echo "<table border="0" cellspacing="3" cellpadding="3">n";

    echo "<tr><td>Type The Letters You See Below Into the Box</td></tr>n";

    echo "<tr><td align="center"><img src="image.php"></td></tr>n";

    echo "<tr><td align="right"><input type="text" name="image"></td></tr>n";

    echo "<tr><td align="right"><input type="submit" name="submit" value="Check CAPTCHA"></td></tr>n";

    echo "</table></form>n";

    }else{

    $image = $_POST['image'];

    if ($image == $_SESSION['string']){

    echo "<b>Great success! </b>n";

    }else{

    echo "<em>Failure! </em>n";

    }

    }

    ob_end_flush();

    ?>

  2. Source Code
    ===========
    image.php
    ==========

    <?php
    session_start();
    $img=imagecreatetruecolor(80,30);
    $white=imagecolorallocate($img,255,255,255);
    $black=imagecolorallocate($img,255,0,0);
    $red=imagecolorallocate($img,255,0,0);
    $pink=imagecolorallocate($img,200,0,100);
    $grey=imagecolorallocate($img,150,150,150);
    function randomString($length)
    {
    $chars="abcdefghijklmnopqrstuvwxyz0123456789";
    srand((double)microtime()*1000000);
    $str="";
    $i=0;
    while($i<=$length)
    {
    $num=rand()%33;
    $tmp=substr($chars,$num,1);
    $str=$str.$tmp;
    $i++;
    }
    return $str;
    }
    imagefill($img,0,0,$white);
    $string=randomString(rand(7,10));

    $_SESSION['string']=$string;

    imagettftext($img,11,0,10,20,$black,"calibri.ttf",$string);
    //imagettftext($img,11,0,11,21,$grey,"calibri.ttf",$string);

    header("Content-type: image/png");
    imagepng($img);
    imagedestroy($img);
    ?>

    captcha.php
    ===========
    <?php
    error_reporting(E_ERROR);
    ob_start();
    session_start();
    if(!$_POST['submit'])
    {
    echo "<form method="post" action="captcha.php">n";
    echo "<table border="2" cellspacing="3" cellspacing="3">n";
    echo "<tr><td>Type the Letters You See Below into the Box </td></tr>";
    echo "<tr><td align="center"><img src="image.php"></td></tr>";
    echo "<tr><td align="right"><input type="text" name="image"></td></tr>";
    echo "<tr><td align="right"><input type="submit" name="submit" value="Check Captcha"></td></tr>";
    echo "</table></form>";
    }
    else
    {
    $image=$_POST['image'];
    if($image==$_SESSION['string'])
    {
    echo "<b>Great Success</b>n";
    }
    else
    {
    echo "<em>Failure</em>n";
    }
    }
    ob_end_flush();
    ?>

  3. I went to your website clicked 'video tutorials' with no problems. the address in the address bar reads phpsquad,net/index,php?act=vid_tuts. i then click on a tutorial such as 'part 1'. the address is then appended with #f_p1 but nothing happens. I am having this same sissue with my e-mail. I goto delete something, the address is appended with # and nothing happens. wtf is going on?

  4. That's false. You SHOULD include at least a couple of random lines and possibly use a random background/foreground color because it is possible to make a program to determine the letters in a captcha otherwise. It's very difficult (involves scanning each pixel), but a similar code is used for cham based aimbot hacks in many first person shooter games. Because of this, random lines and random coloring is almost a must in order to prevent compromising by a good botting tool.

  5. Can I ask for help? I copied all the codes that you typed in my editor but the problem is there's no image that appears on my website. Instead it only displayed the codes that I did in the editor. Please and thank you. 🙂

Comments are closed.

Copyright © All rights reserved. | Newsphere by AF themes.