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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts

    GD Image Validation [CAPTCHA]

    I quickly wrote up a little class to do GD Image Validation, as I've seen a few people asking about it. GD Image Validation is useful for forms where you don't want automated submissions, such as email contact forms or registration forms. The class generates a random value and outputs it as an image, also setting it to a session variable to be compared with the user-entered data. I've posted the class, and the example below.

    PHP Code:
    <?php
    // Velox Letum (2005)
    // elementation@gmail.com

    class gd_verification {
        var 
    $im NULL;
        var 
    $string NULL;
        var 
    $height;
        var 
    $width;

        function 
    gd_verification ($height 150$width 35$sid NULL) {
            if (
    $sid != NULL) {
                
    session_name($sid);
            }

            
    // Set image height/width
            
    $this->height $height;
            
    $this->width $width;
            
            
    // Start session
            
    session_start();
        }
        function 
    generate_string () {
            
    // Create random string
            
    $this->string substr(sha1(mt_rand()), 176);
            
            
    // Set session variable
            
    $_SESSION['gd_string'] = $this->string;
        }
        function 
    verify_string ($gd_string) {
            
    // Check if the original string and the passed string match...
            
    if ($_SESSION['gd_string'] === $gd_string) {
                return 
    TRUE;
            } else {
                return 
    FALSE;
            }
        }
        function 
    output_input_box ($name$parameters NULL) {
            return 
    '<input type="text" name="' $name '" ' $parameters ' /> ';
        }
        function 
    create_image () {
            
    // Seed string
            
    $this->generate_string();

            
    $this->im imagecreatetruecolor($this->height$this->width); // Create image
            
            // Get width and height
            
    $img_width imagesx($this->im);
            
    $img_height imagesy($this->im);
            
            
    // Define some common colors
            
    $black imagecolorallocate($this->im000);
            
    $white imagecolorallocate($this->im255255255);
            
    $red imagecolorallocatealpha($this->im2550075);
            
    $green imagecolorallocatealpha($this->im0255075);
            
    $blue imagecolorallocatealpha($this->im0025575);
            
            
    // Background
            
    imagefilledrectangle($this->im00$img_width$img_height$white);
            
            
    // Ellipses (helps prevent optical character recognition)
            
    imagefilledellipse($this->imceil(rand(5,$img_width 5)), ceil(rand(0,$img_height)), 3030$red);
            
    imagefilledellipse($this->imceil(rand(5,$img_width 5)), ceil(rand(0,$img_height)), 3030$green);
            
    imagefilledellipse($this->imceil(rand(5,$img_width 5)), ceil(rand(0,$img_height)), 3030$blue);

            
    // Borders
            
    imagefilledrectangle($this->im00$img_width0$black);
            
    imagefilledrectangle($this->im$img_width 10$img_width 1$img_height 1$black);
            
    imagefilledrectangle($this->im000$img_height 1$black);
            
    imagefilledrectangle($this->im0$img_height 1$img_width$img_height 1$black);
            
            
    imagestring ($this->im5intval(($img_width - (strlen($this->string) * 9)) / 2),  intval(($img_height 15) / 2), $this->string$black); // Write string to photo
        
    }
        function 
    output_image() {
            
    $this->create_image(); // Generate image

            
    header("Content-type: image/jpeg"); // Tell the browser the data is a JPEG image
            
            
    imagejpeg($this->im); // Output Image
            
    imagedestroy($this->im); // Flush Image
        
    }
    }
    ?>
    Then to use it I made an example (two files), the form:
    PHP Code:
    <?php
    // Velox Letum (2005)
    // elementation@gmail.com

    // Require the class code...
    require ('gd_image_verify.php');

    // Initialize class
    $gd = new gd_verification();

    if (isset(
    $_POST['submit'])) {
        if (
    $gd->verify_string($_POST['gd_string'])) {
            
    $result 'User input matches image string.';
        } else {
            
    $result 'User input does not match image string.';
        }
    }
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
    <title>GD Image Verification Class Example</title>
    <head>
    <body>
        <div style="text-align:center;">
            <h1>GD Image Verification Class Example</h1>
            <? echo $result?><br /><br />
            <form name="gd_verify" method="POST" action="<? echo $PHP_SELF?>">
                <img src="gd_image.php" alt="Verification Image" /><br /><br />
                <? echo $gd->output_input_box('gd_string'); ?><br /><br .>
                <input type="submit" name="submit" value="Submit" />
            </form>
        </div>
    </body>
    </html>
    and the image output:
    PHP Code:
    <?php
    // Velox Letum (2005)
    // elementation@gmail.com

    // Require the class code...
    require ('gd_image_verify.php');

    // Initialize class
    $gd = new gd_verification();

    // Output image
    $gd->output_image();
    ?>
    Enjoy.
    Last edited by Velox Letum; 12-06-2005 at 03:58 AM.
    "$question = ( to() ) ? be() : ~be();"

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Looks spiffy. I think it can be done alot quicker with other methods, but deffinately for a person running a site based on class files and function files this would be perfect. I'm going to have to give this a try.

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    This image verification is called CAPTCHA. At first I thought this would validate image files...
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Theres only problem i can see is that you are using session_register(), which is now deprecated.

    @Element:
    Really, when you break it down, it doesn't get much simpler than this. Classes are very useful for containing lots of code in an object. If you needed image validation on 2 pages, you just load this class into both pages and its all there for you, with no additional code.

  • #5
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I read the page on session_register() and removed it...I thought it was a way to initialize session variables so an E_NOTICE wouldn't pop up. I changed it.
    "$question = ( to() ) ? be() : ~be();"

  • #6
    Regular Coder
    Join Date
    Aug 2005
    Posts
    257
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Looks great but Is possible to validate the INPUT without SUBMIT? I talk about validate the INPUT and the CODE without $_POST I yalk about validate like a normal input. Regards

  • #7
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why go to the overhead of creating a CAPTCHA Image that is supposed to be only human readable, when you are sending the value of the CAPTCHA in a cookie (plain text) that is machine readable?

    PHP Code:
     function generate_string () {
            
    // Create random string
            
    $this->string substr(sha1(mt_rand()), 176);
            
            
    // Set session variable
            
    $_SESSION['gd_string'] = $this->string;
        } 
    Am I missing something?

    What you should do is link a session id to the CAPTCHA value, and save this value on the server side (db, file etc). That way the client machine never gets the value of the CAPTCHA, only the session id.

    You can then use the session id to retrieve the value of the CAPTCHA when validating the clients response.
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Session data is not sent to the user... only the session id is.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #9
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by marek_mar
    Session data is not sent to the user... only the session id is.
    Oh wow thanks!
    Somehow I was seeing $_COOKIE instead of $_SESSION. Urrggg, I blame the coffee.
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog

  • #10
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi there, I'm a bit new on this, but I would like to also use this for my form as in the past they have spammed me a lot through traditional forms and even the hosting company made me take out the form.

    So I have implemented this supercode in my website, but seems not to create the image, what can be the problem? I have my gd_enabled on the server.

    In my info.php states: 'with-gd' '--enable-gd-native-ttf' '--without-gdbm'

    I supose the server has enough to run with this code right?

    many zanx!

  • #11
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    welcome here!

    it always helps if you show us the code that you used...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #12
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this
    PHP Code:
    action="<? echo $PHP_SELF?>"
    should be like
    PHP Code:
    action="<?php echo $_SERVER['PHP_SELF']; ?>
    i think
    Last edited by jr_yeo; 08-01-2006 at 06:07 PM.
    Please don't mind me. I'm just a noob.

  • #13
    Registered User
    Join Date
    Oct 2004
    Posts
    592
    Thanks
    0
    Thanked 1 Time in 1 Post
    Great thanks.

    I'm now collecting useful php classes object.

    Then I will build Php Class Framework which will include all the author info.

    I have a plan coz I want it like Ajax framework.

    Any comments will be welcomed. pm me. thanks.

  • #14
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question... please help me

    I am not very experienced at scripting with PHP, but I'm trying to get this to work within Formmail. Is that possible? My forms at my web site get a ton of Spam, and I was hoping to get rid of that without having to re-invent the wheel. Is there a way to do this? If you go to http://www.cyberscape.ca/contact_us.php you can see what I am doing (and maybe what I'm doing wrong).

    Thanks in advance!

  • #15
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It looks like you managed to implement it alright, or are you still struggling?

    jr_yeo: That file is a non-issue as it was only an example written straight into the text editor here on CF.
    "$question = ( to() ) ? be() : ~be();"


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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