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 4 of 4
  1. #1
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,995
    Thanks
    15
    Thanked 233 Times in 233 Posts

    Easy Captcha Script

    This may help in the fight against spam. It uses two generated images and a drop down list with numbers, coded as utf-8 or ascii characters to make it harder for a spambots to read, allows for rejection after failed attempts. It has quite a few presets, so you can adjust it to your liking. It can be easily converted into a shout box, emailer
    , basic form, etc., you can add inputs but be careful not to confuse it by using the same names as the current ones. The cookie is used fo centering the image, but can be used for other purposes as well. It works best with a monospace font.
    PHP Code:
    <!DOCTYPE html>
    <head>
    <title>Easy Captcha Script</title>
    <style>
    <?php echo '@font-face { font-family: "DejaVuSansMono-Bold"; src: url(http://'.$_SERVER["HTTP_HOST"].'/Captcha/DejaVuSansMono-Bold.ttf)  format("truetype");}';  ?>

    body
    {
    width: 960px;
    margin: 0 auto;
    background: #202020;
    font-family: Comic Sans MS;
    font-size:24px;
    color: white;
    }
    </style>
    </head>
    <body>
    <div>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
     Pellentesque congue gravida magna, in accumsan velit aliquam nec.
     Suspendisse condimentum auctor quam nec venenatis.
     Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
     Phasellus feugiat consectetur vulputate.
     In hac habitasse platea dictumst.
     Vestibulum vestibulum tincidunt laoreet.
     Cras tincidunt, tortor at pellentesque aliquet, dui nibh lacinia magna, lacinia pulvinar libero risus nec felis.
     Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
     Sed a velit et diam pulvinar ultrices.
     Cras pharetra, sapien eget egestas tristique, orci risus euismod mi, eget pretium nulla tortor at velit.
     Sed congue nibh et mauris molestie fringilla.
     Ut enim lectus, molestie ut malesuada vitae, elementum eget odio.
     Quisque feugiat arcu vitae sem semper quis condimentum velit pellentesque.
    </div>
    <?php  include "./login.php"?>
    <script type="text/javascript">
    if(document.getElementById("myform"))
    {
    mf=document.getElementById("myform");
    mf.style.right=(window.innerWidth-mf.offsetWidth)/2+"px";mf.style.top=(window.innerHeight-mf.offsetHeight)/2+"px";
    }
    function setCookie(c_name,value)
        {
        document.cookie=c_name+ "=" +escape(value);
        } 
    </script>
    </body>
    </html>

    <?php
    if(!isset($_SESSION)) session_start();
    if(!isset(
    $_SESSION['inc'])) $_SESSION['inc']=0// Incremented when a failed login attempt occurs.
    // Presets.
    $font_size=20;
    $font_face 'DejaVuSansMono-Bold';
    $bkg = array(0,255,255); // Input background color. Use the rgb colorset.
    $color=array(100,0,0); // Input font color. Use the rgb colorset.
    $border_color="#ffffff"// Borders and fieldset color. Use any format.
    $ol 24;  // Number of options.
    $dl 6;   // Number of digits. If you change this, you must change the number of digits on line 44.
    $img_bkg='rgb('.$bkg[0].','.$bkg[1].','.$bkg[2].')';
    $w=$font_size*$dl;

    $style1='style="text-align:center;font-family:'.$font_face.';background:'.$img_bkg.';color:rgb('.$color[0].','.$color[1].','.$color[2].');font-size:'.$font_size.'px;border:2px solid '.$border_color.';border-radius: 5px;margin-bottom:5px;"';

     
    if(isset(
    $_POST['spare']))
    {
    // Fetch the username/password info and include it in the if statement.

    $inputs=$_POST['myin'];
    if( 
    $_SESSION['rand0']==$inputs[2] && $_SESSION['rand1']==$inputs[3] && $_SESSION['inc']<)
    {
    $_SESSION['inc']=0;
    setcookie('loggedin',"YES"2147483647 ); 
    $_SESSION['usr_login']=true;
    header("location: ./index.php");
    exit;
    }
    else
    {
    $_SESSION['inc']++;
    header("location: ./index.php");
    exit;
    }
    }
    //print_r($_SESSION);

    if(!isset($_SESSION['usr_login']) && $_SESSION['inc']<4)
    {
    for(
    $i=0;$i<$ol;$i++)
    {
    $_SESSION['chars'][$i]=rand(100000,999999);
    $chars=$_SESSION['chars'];
    $char=str_split($chars[$i]);
    for(
    $j=0;$j<$dl;$j++)
    {
    //$ascii[$j]="&#".ord($char[$j]).";"; // Use this for ascii.
    $ascii[$j]="&#x".dechex(ord($char[$j])).";"// Use this for utf-8
    }
    $vals=implode($ascii);
    $opt[$i]='<option value='.$i.' > '.$vals.' </option>
    '
    ;
    }
    $opts=implode($opt);
    $_SESSION['rand0']=rand(0,$ol-1);
    $rand0=$chars[$_SESSION['rand0']];
    $_SESSION['rand1']=rand(0,$ol-1);
    $rand1=$chars[$_SESSION['rand1']];

    $_SESSION['params']=array($rand0,$rand1,$w,$font_size,$bkg[0],$bkg[1],$bkg[2],$color[0],$color[1],$color[2],$font_face);

    echo 
    '<div style="position:fixed;top:0;left:0;background:black;width:100%;height:100%;z-index:auto;opacity: 0.8;"></div>
    <div style="position:absolute;top:50px;right:200px;font-size:'
    .$font_size.'px;z-index:20;" id="myform">
    <form action="./login.php" method="post" >
    <fieldset style="font-family:'
    .$font_face.';background:transparent;z-index:20;border:2px solid '.$border_color.';color:'.$border_color.';font-size:'.$font_size.'px;border-radius:10px;">
    <legend>Match the numbers</legend>
    Username:<input type="text" '
    .$style1.' name="myin[]" size=12 /><br />
    Password:<input type="text" '
    .$style1.' name="myin[]" size=12 /><br />
    <input type="hidden" name="spare" />
    <table><tbody><tr>
    <td><img src="./image.php" style="border:2px solid '
    .$border_color.';border-radius:4px;" /></td>
    <td><img src="./png1.php" style="border:2px solid '
    .$border_color.';border-radius:4px;" /></td>
    </tr><tr>
    <td><select name="myin[]" '
    .$style1.' >
    '
    ;
    echo 
    $opts;
    echo 
    '</select></td>
    <td><select name="myin[]" '
    .$style1.' >
    '
    ;
    echo 
    $opts;
    echo 
    '</select></td>
    </tr></tbody></table>
    <input type="submit" value="Login" '
    .$style1.' />
    Forever login.<input type="checkbox" name="myin[]" value="YES" />
    <br />
    Uses a permanent cookie.
    <br /><!-- Place any text on this line. -->
    </fieldset>
    </form>

     </div>'
    ;
    }

    if(isset(
    $_COOKIE['loggedin']))
    {
    echo 
    "HELLO";
    sleep(5);
    header("location: ../index.php");
    }


    ?>

    <?php
    header
    ('Content-Type: image/png');
    $p=$_SESSION['params'];
    $im imagecreatetruecolor($p[2],$p[3]);
    $bkg imagecolorallocate($im$p[4], $p[5], $p[6] );
    $font_color imagecolorallocate($im$p[7], $p[8], $p[9] );
    imagefilledrectangle($im0$p[2], $p[3], $bkg);
    $font="./".$p[10].".ttf";
    // Name one file png0.php, the other png1.php.
    // The only difference is in the line below, 
    // for png0.php the value is 0, for png1.php the value is 1.
    $text=" ".$p[0];  
    imagettftext($im160020$font_color$font$text);
    imagepng($im);
    imagedestroy($im);
    ?>
    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.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by DrDOS View Post
    PHP Code:
    <!DOCTYPE html>
    ...
    </html>

    <?php
    if(!isset($_SESSION)) session_start();
    You do realise that you can't do that don't you?

    You've already started your html output and you're then trying to start a session AFTER which will need to send a session cookie in the output headers that are sent BEFORE the html.

    This line:
    if(!isset($_SESSION)) session_start();

    Should be at the top of your code and even then you should just call session_start() directly and not bother with the if / isset thing as it is using extra CPU power that will always be guaranteed to return a true result as there is no $_SESSION array populated until you call session_Start().
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,995
    Thanks
    15
    Thanked 233 Times in 233 Posts
    Quote Originally Posted by tangoforce View Post
    You do realise that you can't do that don't you?

    You've already started your html output and you're then trying to start a session AFTER which will need to send a session cookie in the output headers that are sent BEFORE the html.

    This line:
    if(!isset($_SESSION)) session_start();

    Should be at the top of your code and even then you should just call session_start() directly and not bother with the if / isset thing as it is using extra CPU power that will always be guaranteed to return a true result as there is no $_SESSION array populated until you call session_Start().
    You do realize that you are looking at three different pages, don't you ? It's true the session_start should be at the beginning of the HTML page, and I put it in the include because I wrote it as a simple to use drop-in where you only have to add the one include line. People who are more wise to things can easily change it to their liking. And it does work very well, even though it's incomplete, because it isn't really configured for a particular use. The amount of cpu cycles used by the if(!isset statement is negligible compared to those used by bots uploading spam.
    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.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by DrDOS View Post
    You do realize that you are looking at three different pages, don't you ?
    Er actually no I didn't but that would at least explain it!

    Maybe multiple [php] tag sets should have been used otherwise there may be others who also don't understand your contribution
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

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