Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,089
    Thanks
    15
    Thanked 246 Times in 246 Posts

    Easy Captcha script.

    Here's a captcha script where you have to select an image from a list to complete the captcha. OK, got it working on all browsers tested now.

    You can view it in action here.

    http://www.myphotowizard.net/Captcha/index.php

    Here's the index page.
    PHP Code:
    <?php @session_start(); ?>
    <!DOCTYPE html>
    <head>
    <title>Easy Captcha Script</title>
    <style>
    <?php echo "\n    @font-face { font-family: \"DroidSansMono\"; src: url(http://".$_SERVER["HTTP_HOST"]."/Captcha/DroidSansMono.ttf)  format(\"truetype\");} \n"?>
    body
    {
    width: 800px;
    margin: 0 auto;
    background: black;
    font-family: DroidSansMono;
    font-size:24px;
    color: white;
    }
    #container
    {
    margin-top: 40px;
    }
    input, select
    {
    height: 32px;
    margin: 4px;
    font-size:22px;
    color: white;
    text-align: center;
    background: darkblue;
    border: 1px solid red;
    }
    #showme
    {
    margin-left: 20px;
    width: 120px;
    height: 120px;
    background: white;
    padding: 20px;
    border: 1px solid red;
    }
    fieldset{border-radius: 10px;}
    </style>
    </head>
    <body>
    <div id="container">
    <?php
    if (isset($_POST['myname']) && isset($_SESSION['capture']))
        {
        
    $myname $_POST['myname'];
        
    $mymail $_POST['mymail'];
        
    $mymessage $_POST['mymessage'];
        
    $capture $_POST['capture'];
        
    $choice $_POST['my_choice'];
    if (
    $_SESSION['capture'] == $capture AND $_SESSION['num'] == $choice)
        {
        
    // Put what you want done here.
        
    echo "Your message has been received.";
        }
        else echo 
    "You entered an incorrect capture";
        }
        
    function 
    makeCapture()
        {
        
    $new_capture rand(100000,999999);
        
    $_SESSION['tmpnum']=file_get_contents("./tmp/tmpnum");
        
    $_SESSION['tmpimg']="./tmp/tmpimg-".$_SESSION['tmpnum'].".png";
        
    $_SESSION['tmpnum']++;
        
    $_SESSION['tmpnum']=($_SESSION['tmpnum']%16);
        
    file_put_contents("./tmp/tmpnum" $_SESSION['tmpnum']);
        
    $_SESSION['icons']=glob("./icons/*.png");
        
    $_SESSION['ct']=count($_SESSION['icons']);
        
    $_SESSION['num']=rand(0,$_SESSION['ct']-1);
        
    $_SESSION['tmp']="./tmp/temp-".$_SESSION['tmpnum'].".png";
        
    $tmpcapt=$_SESSION['tmp'];
        
    $_SESSION['capture']=$new_capture;
        `
    convert -size 144x36 xc:darkblue -bordercolor red -border 1 '$tmpcapt'`;
        `
    mogrify -gravity center -fill white -pointsize 24 -font ./DroidSansMono.ttf -page 0x0 -annotate +0+0 '$new_capture' '$tmpcapt'`;
        
    copy($_SESSION['icons'][$_SESSION['num']] , $_SESSION['tmpimg']);
        
    //return $_SESSION['tmpimg'];
        
    }
        
    makeCapture();
        
    ?>
    <form action="./index.php" method="post" enctype="multipart/form-data" name="convert">
        <fieldset>
        Your name: <input type="text" name="myname" size=20 ><br />
        Your email:<input type="text" name="mymail" size=20 ><br />
        <table><tbody>
        <tr>
        <td>Enter number here:&nbsp;</td><td><input type="text" name="capture" size=8 /></td>
        </tr><tr>
        <td>Enter this number:</td><td><img src="<?php echo $_SESSION['tmp']; ?>"></td>
        </tr>
        </tbody></table>
        
        <p id="showme"><img src="<?php echo $_SESSION['tmpimg']; ?>"></p>
        Identify this image:<br /><select name="my_choice">
    <?php
        
    for($i=0;$i<$_SESSION['ct'];$i++)
        {
        
    $entry=basename($_SESSION['icons'][$i]);
        
    $entry=str_replace(".png","",$entry);
        echo 
    "    <option value=\"".$i."\">".$entry."</option>\n";
        }
        
    ?>
        </select><br />
        <textarea name="mymessage" id="msg" cols=36 rows=16 ></textarea><br />Enter your message here<br/>
        <input type="submit" value="Send message">
        </fieldset>
    </form>
    </div>
    </body>
    </html>
    You can download the whole package here, images and all.
    In addition to the index you need two subfolders, icons, with the image files, set to png but can be changed, and tmp, containing a text file named tmpnum and having a number of 0 as content.

    http://www.myphotowizard.net/Captcha.zip
    Last edited by DrDOS; 08-14-2013 at 06:31 PM. Reason: Got it working on the web.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •