Menu

Pop-up

Help
Julian
2010-11-23
2013-06-03
  • Julian

    Julian - 2010-11-23

    Does anyone now how to make the invalid captcha warning come up as a pop up and not a new screen as it currently does. I have tried replacing this code but i cannot get it to work:

    }
    else {
    echo "Invalid Captcha String.";
    }
    

    Thanks

     
  • TNTEverett

    TNTEverett - 2010-11-23

    Yes I do but it is not trivial.  I will try to put together some directions but again, I have to warn you it requires quite a bit of code changes.  Are you up for it?

     
  • TNTEverett

    TNTEverett - 2010-11-23

    Here are the steps required:
    1.) Separate the "CaptchaSecurityImages.php" file into 2 parts ("CaptchaSecurityCode.php" and "CaptchaSecurityImage.php").
    2.) Rename the "form.html" file into "form.php".
    3.) Add a "validateCaptcha()" function to the "form.php" file.
    4.) Add a function call to "validateCaptcha()" in the "form.php" file.
    5.) Add the "CaptchaSecurityCode.php" as an include statement at the top of the "form.php" file.
    6.) Change the previous instance of "CaptchaSecurityImages.php" to the new "CaptchaSecurityImage.php" in the "form.php" file.
    This sounds simple enough but if you get it wrong the "form.php" file may not even be displayed.

     
  • Julian

    Julian - 2010-11-24

    Thanks for the instructions, I shall give it a go.

    Regards

    Julian

     
  • TNTEverett

    TNTEverett - 2010-11-24

    That was not intended to be instructions, just an outline of steps required.  As long as you are not intimidated by the scope of the work I can begin to put together some instructions.  Is this what you want to do?

     
  • Julian

    Julian - 2010-11-24

    It is yes, so I would be gratefull if you could provide some instructions.

    Regards

    Julian

     
  • TNTEverett

    TNTEverett - 2010-11-26

    Create this file "CaptchaSecurityImage.php".

    <?php
    session_start();
    /*
    * File: CaptchaSecurityImages.php
    * Author: Simon Jarvis
    * Copyright: 2006 Simon Jarvis
    * Date: 03/08/06
    * Updated: 23/11/06
    * Requirements: PHP 4/5 with GD and FreeType libraries
    * Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * as published by the Free Software Foundation; either version 2
    * of the License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details:
    * http://www.gnu.org/licenses/gpl.html
    *
    */
    class CaptchaSecurityImage {
    var $font = 'monofont.ttf';
    function CaptchaSecurityImage($width='120',$height='40',$characters='6') {
    $code = $_SESSION;
    /* font size will be 75% of the image height */
    $font_size = $height * 0.75;
    $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
    /* set the colours */
    $background_color = imagecolorallocate($image, 255, 255, 255);
    $text_color = imagecolorallocate($image, 20, 40, 100);
    $noise_color = imagecolorallocate($image, 100, 120, 180);
    /* generate random dots in background */
    for( $i=0; $i<($width*$height)/3; $i++ ) {
    imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
    }
    /* generate random lines in background */
    for( $i=0; $i<($width*$height)/150; $i++ ) {
    imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
    }
    /* create textbox and add text */
    $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
    $x = ($width - $textbox)/2;
    $y = ($height - $textbox)/2;
    imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
    /* output captcha image to browser */
    imagejpeg($image);
    imagedestroy($image);
    }
    }
    $width = isset($_GET) ? $_GET : '120';
    $height = isset($_GET) ? $_GET : '40';
    $characters = isset($_GET) ? $_GET : '6';
    header('Content-Type: image/jpeg');
    $captcha = new CaptchaSecurityImage($width,$height,$characters);
    ?>

     
  • TNTEverett

    TNTEverett - 2010-11-26

    Create this file "CaptchaSecurityCode.php".

    <?php
    session_start();
    /*
    * File: CaptchaSecurityImages.php
    * Author: Simon Jarvis
    * Copyright: 2006 Simon Jarvis
    * Date: 03/08/06
    * Updated: 23/11/06
    * Requirements: PHP 4/5 with GD and FreeType libraries
    * Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * as published by the Free Software Foundation; either version 2
    * of the License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details:
    * http://www.gnu.org/licenses/gpl.html
    *
    */
    function generateCode($characters) {
    /* list all possible characters, similar looking characters and vowels have been removed */
    $possible = '23456789bcdfghjkmnpqrstvwxyz';
    $code = '';
    $i = 0;
    while ($i < $characters) {
    $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
    $i++;
    }
    return $code;
    }
    $characters = isset($_GET) ? $_GET : '6';
    $newcode=generateCode($characters);
    $_SESSION=$newcode;
    ?>

     
  • TNTEverett

    TNTEverett - 2010-11-26

    Rename your "form.html" to "form.php".  Add this to the very top of the file.

    <?php
    include('CaptchaSecurityCode.php');
    ?>

     
  • TNTEverett

    TNTEverett - 2010-11-26

    Find these comment lines in the form.php file
    <!- expand/collapse function ->
    <!- validate ->
    They occur together and just before these lines.
    <SCRIPT type=text/javascript>
    <!-
    function validateField(fieldId, fieldBoxId, fieldType, required)

    Put the following code between the two comment lines.

    <?php
    print '
          <SCRIPT type=text/javascript>
          <!-
    function validateCaptcha(fieldId, fieldBoxId, fieldType, required)
    {
    fieldBox = document.getElementById(fieldBoxId);
    fieldObj = document.getElementById(fieldId);

            sess_val="'.$_SESSION.'";
    if(required == 1 && fieldObj.value == "")
    {
    fieldObj.setAttribute("class","mainFormError");
    fieldObj.setAttribute("className","mainFormError");
    fieldObj.focus();
    alert("Please correct the errors.  Fields marked with an asterisk (*) are required");
    return false;
    }
    else
    {
    if(fieldObj.value == sess_val) {
    return true;
    }
    else {
    alert("Invalid Captcha String: " + fieldObj.value);
    return false;
    }
    }
    }
          //->
    </SCRIPT>';
    ?>

     
  • TNTEverett

    TNTEverett - 2010-11-26

    Find the following line in the form.php file.

    Type the following:&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>For security purposes, please type the letters in the image.</span></a><BR><img src="CaptchaSecurityImages.php" />

    Change "CaptchaSecurityImages.php" to "CaptchaSecurityImage.php".

    Type the following:&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>For security purposes, please type the letters in the image.</span></a><BR><img src="CaptchaSecurityImage.php" />

     
  • TNTEverett

    TNTEverett - 2010-11-26

    Find the following lines in the form.php file.

    if(retVal == false)
    {
    alert('Please correct the errors.  Fields marked with an asterisk (*) are required');
    return false;
    }

    Right after these lines, insert the following code.

    if (validateCaptcha('security_code','captchaForm','textarea',1) == false)
    retVal=false;

     
  • TNTEverett

    TNTEverett - 2010-11-26

    Once you have completed all these steps, here is what should happen.
    1.) Test it but leaving the captcha value blank.  The box should turn pink like any other required field when you leave it blank.  You will get a popup that says "Please correct the errors.  Fields marked with an asterisk (*) are required".
    I now see that this is not quite accurate so you should modify this alert comment in step 4 to read something like "The Captcha field is required. Please correct this error.".
    2.) Test it by putting in the captcha value with an error. You will get a popup that says "Invalid Captcha String: {captcha value entered}".
    3.) Test it by putting in the exact captcha value.  The form will continue without any popup related to the capcha value. 

    Let me know if this works as I say it should.  I don't think I have left anything out. 

     
  • Julian

    Julian - 2010-11-26

    Thanks for the detailed instructions. I'll let you know how I get on.

    Regards.

     
  • Julian

    Julian - 2010-11-28

    Ok I think I followed these instructions carefully. Here is an error message that I am now getting displayed:

    Warning: session_start() : Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\test_site\enrol.php:10) in C:\xampp\htdocs\test_site\CaptchaSecurityCode.php on line 2

    Warning: session_start() : Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\test_site\enrol.php:10) in C:\xampp\htdocs\test_site\CaptchaSecurityCode.php on line 2

     
  • TNTEverett

    TNTEverett - 2010-11-28

    I'm not sure what you are doing but it is related to this file "enrol.php". 
    The "CaptchaSecurityCode.php" file is meant to be inserted into the very top (line 2 right after "<?php") of your form file. 
    The error starts on line 10 of your "enrol.php". 

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.