#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Weird captcha code


    This tiny captcha code is kind of weird. Only works in Firefox but not IE (6,8)
    Hope someone may figure it out what's going on?
    Or you may download this code from Author. (demo included)
    codefuture.co.uk

    exmple.php
    PHP Code:
    <?php
        
    if(isset($_POST['setCaptcha'])){
            include_once(
    './captcha/cf.captcha.php');
            if(!
    check_captcha($_POST['setCaptcha'])){
                echo 
    'CAPTCHA CHECK: <span style="color:red">'.$error_captcha.'</span>';
                
    //exit();
            
    }else{
                echo 
    'CAPTCHA CHECK: <span style="color:green">Correct</span>';
                
    //proceed
            
    }
        }
    ?>

    <form  method="post" action="" class="center">
        <br/><img id="captcha_img" src="./captcha/cf.captcha.php?img=<?=time();?>" /><br/>
        <a href="#" onclick="document.getElementById('captcha_img').src = './captcha/cf.captcha.php?img=' + Math.random(); return false">Reload Captcha</a><br/>
        <label for="setCaptcha">Verification code: </label><br/>
        <input name="setCaptcha" type="text" id="setCaptcha" size="24" class="textbox" /><br/><br/>
        <input type="submit" value="Send" /><br/>
    </form>

    cf.captcha.php

    PHP Code:
    <?php
    //
    //   CF Captcha v0.9
    //   -------------------------------
    //
    //   Author:    codefuture.co.uk
    //   Version:   0.9
    //   Date:      28-Apr-10
    //
    //   download the latest version from - codefuture.co.uk/projects/captcha/
    //   Copyright (c) 2010 codefuture.co.uk
    //
    //////////////////////////////////////////////////////////////////////////////////////////////////

    //debug
    //    error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

    //////////////////////////////////////////////////////////////////////////////////////////////////
    // settings

    // Cookie timer
        
    define('COOKIE_TIMER');

    // Cookie Salting Pick a random code
        
    define('SALTING''aeb1021e49282375948ac10004c2eb53ef9070e0');

    // check_captcha() Error Messages
        
    define('ERROR_MESSAGE_CAPTCHA',                'Verification code can\'t be blank');
        
    define('ERROR_MESSAGE_CAPTCHA_INVALID',        'Verification code is invalid');
        
    define('ERROR_MESSAGE_CAPTCHA_TIMED_OUT',    'Sorry the verification code is only valid for '.COOKIE_TIMER.' minutes');
        
    define('ERROR_MESSAGE_CAPTCHA_COOKIE',        'No captcha cookie given. Make sure cookies are enabled.');


    //////////////////////////////////////////////////////////////////////////////////////////////////
    // Check Captcha


    /**
     * Holds any errors from check_captcha
     * you could use it like this
     * <div style="color:red"><?=$error_captcha;?></div>
     */
        
    $captcha_error null;

    /**
     * check_captcha()
     *
     * @param string $captcha
     * @param boolean $timer (on|off)(true|false)
     * @return boolean
     */
    function check_captcha($captcha,$timer true){
        global 
    $error_captcha;

        
    $captcha htmlspecialchars(stripslashes(trim($captcha)));

        if(empty(
    $captcha)){
            
    $error_captcha ERROR_MESSAGE_CAPTCHA;
            
    remove_cookie();
            return 
    false;

        }elseif (isset(
    $_COOKIE['Captcha']) ){
            list(
    $Hash$Time) = explode('.'$_COOKIE['Captcha']);
            if ( 
    md5(SALTING.$captcha.$_SERVER['REMOTE_ADDR'].$Time) != $Hash ){
                
    $error_captcha ERROR_MESSAGE_CAPTCHA_INVALID;
                
    remove_cookie();
                return 
    false;
            }elseif( (
    time() - COOKIE_TIMER*60) > $Time && $timer){
                
    $error_captcha ERROR_MESSAGE_CAPTCHA_TIMED_OUT;
                
    remove_cookie();
                return 
    false;
            }

        }else{
            
    $error_captcha ERROR_MESSAGE_CAPTCHA_COOKIE;
            return 
    false;
        }

        return 
    true;
    }

    function 
    remove_cookie(){
        
    $domain $_SERVER['HTTP_HOST'];
        if ( 
    strtolowersubstr($domain04) ) == 'www.' )
            
    $domain substr($domain4);
        if ( 
    substr($domain01) != '.' )
            
    $domain '.'.$domain;
        
    setcookie('Captcha'''time() - 3600'/',$domain);
        unset(
    $_COOKIE['Captcha']); 
    }


    //////////////////////////////////////////////////////////////////////////////////////////////////
    // make image

    /**
     * the time() at the end of the address is just to keep the image from being cached
     * <img id="captcha_img" src="./captcha/cf.captcha.php?img=<?=time();?>" />
     *
     */
        
    if(isset($_GET['img'])){
            
    $capt = new captcha;
    //        $capt->transparent_bg(false);
    //        $capt->bg_color('cccccc');
            
    $capt->display();
            exit();
        }

    //////////////////////////////////////////////////////////////////////////////////////////////////
    // captcha Class


    class captcha {

        private 
    $UserString;
        private 
    $transparent true;
        private 
    $bg_color 'ffffff';

        private function 
    font(){
            switch(
    rand(1,2)){
                case 
    1: return dirname(__FILE__).'/font/arial.ttf'; break;
                case 
    2: return dirname(__FILE__).'/font/verdana.ttf'; break;
                default : return 
    dirname(__FILE__).'/font/arial.ttf'; break; 
            }
        }

        public function 
    transparent_bg($var){$this->transparent = ($var != true false true);}
        public function 
    bg_color($var){$this->bg_color $var;}

        private function 
    LoadPNG(){
            
    $im imagecreatetruecolor(13035);
            
    $rgb $this->html2rgb($this->bg_color);
            
    $bgcolor imagecolorallocate($im$rgb[0], $rgb[1], $rgb[2]);
            
    imagefill($im00$bgcolor);
            if(
    $this->transparent)imagecolortransparent($im$bgcolor);
            return 
    $im;
        }

        private function 
    task_string(){
            
    $image $this->LoadPNG(); 
            
    $string_a = array("a","b","c","d","e","f","g","h","j","k",
                              
    "m","n","p","r","s","t","u","v","w","x",
                              
    "y","z","2","3","4","5","6","7","8","9");
            
    $x 0;
            for(
    $i 0$i 5$i++){
                
    $x += rand(16,18);
                
    $temp $string_a[rand(0,29)];
                
    $this->UserString .= $temp;
                
    imagettftext($image$this->rand_fontsize(), $this->rand_angle(), $x$this->rand_Y(), $this->rand_color($image), $this->font(), $temp);
            }
            return 
    $image;
        }

        private function 
    rand_color($im){return imagecolorallocate($imrand(0,155), rand(0,155), rand(0,155));}
        private function 
    rand_angle(){return rand(-15,15);}
        private function 
    rand_Y(){return rand(25,30);}
        private function 
    rand_fontsize(){return rand(14,20);}

        private function 
    task_sum(){
            
    $image    $this->LoadPNG(); 
            
    $x        rand(10,20);
            
    $sum    rand(1,3);
            
    $number1$sum != rand(10,99) : rand(1,9);
            
    $number2rand(1,9);
            
    imagettftext($image$this->rand_fontsize(), $this->rand_angle(), $x$this->rand_Y(), $this->rand_color($image), $this->font(),($sum ==3?'':substr($number10,1)));
            
    imagettftext($image$this->rand_fontsize(), $this->rand_angle(), ($x += 15), $this->rand_Y(), $this->rand_color($image), $this->font(),($sum ==3?$number1:substr($number1, -1)) );
            
    imagettftext($image$this->rand_fontsize(), 0, ($x += 20), $this->rand_Y(), $this->rand_color($image), $this->font(),($sum ==1?'+':($sum ==2?'-':'*')));
            
    imagettftext($image$this->rand_fontsize(), $this->rand_angle(), ($x += 18), $this->rand_Y(), $this->rand_color($image), $this->font(), $number2);
            
    imagettftext($image$this->rand_fontsize(), $this->rand_angle(), ($x += 18), $this->rand_Y(), $this->rand_color($image), $this->font(),'=');
            
    imagettftext($image$this->rand_fontsize(), $this->rand_angle(), ($x += 18), $this->rand_Y(), $this->rand_color($image), $this->font(),'?');
            
    $this->UserString = ($sum ==1?$number1+$number2:($sum == 2?$number1-$number2:$number1*$number2));
            return 
    $image
        }

        private function 
    cookie(){
            
    $time time();
            
    $domain $_SERVER['HTTP_HOST'];
            if ( 
    strtolowersubstr($domain04) ) == 'www.' )
                
    $domain substr($domain4);    // Fix the domain to accept domains with and without 'www.'. 
            
    if ( substr($domain01) != '.' )
                
    $domain '.'.$domain;    // Add the dot prefix to ensure compatibility with subdomains
            
    setcookie('Captcha'md5(SALTING.$this->UserString.$_SERVER['REMOTE_ADDR'].$time).'.'.$timenull'/',$domain);
        }

        public function 
    display(){
            switch(
    rand(1,2)){
                case 
    1:     $image $this->task_string();    break;
                case 
    2:     $image $this->task_sum();    break;
                default: 
    $image $this->task_sum();    break;
            }

            
    $this->cookie();
            
    header("Content-type: image/png");
            
    imagepng($image);
        }

        private function 
    html2rgb($color){
            if (
    $color[0] == '#')
                
    $color substr($color1);

            if (
    strlen($color) == 6)
                list(
    $r$g$b) = array($color[0].$color[1],$color[2].$color[3],$color[4].$color[5]);
            elseif (
    strlen($color) == 3)
                list(
    $r$g$b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
            else
                return 
    false;

            
    $r hexdec($r); $g hexdec($g); $b hexdec($b);

            return array(
    $r$g$b);
        }
    }

    ?>
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    The demo on Author's website works both on FF and IE.
    But when I download the code on local machine. It works only in Firefox.
    I thought it's the IE browser setting from the beginning but I already changed all settings and tested under two different OS win7 and xp. It's the same. Only works in Firefox.
  4. #3
  5. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,742
    Rep Power
    9397
    What does "doesn't work" mean? Broken image? Missing cookies? You have to tell us these things because we don't know what's happening on your computer.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,903
    Rep Power
    1045
    ... or just use reCAPTCHA.

    But I know that reinventing the wheel is part of the PHP folklore.
  8. #5
  9. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    501
    Rep Power
    127
    I recently switched from reCAPTCHA to http://visualcaptcha.net/.

    I won't claim it's as secure as the normal reCAPTCHA--I haven't researched it thoroughly--but I wanted something I could use (My brain just has trouble with distorted text...), something that felt a bit fresher, and something which I could update if I wanted.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    177
    Rep Power
    83
    Originally Posted by dansonyun
    This tiny captcha code is kind of weird. Only works in Firefox but not IE (6,8)
    Hope someone may figure it out what's going on?
    There's a couple of things going on here.

    There are several instances in the code where "shortcut" script tags are used. They may or may not work on your server, so they should be changed.
    Where you see
    Code:
    <?
    you should replace it with
    Code:
    <?php
    Where you see
    Code:
    <?=time();?>
    You should replace it with
    Code:
    <?php echo time();?>
    then you should be good to go.
    Now for why it doesn't work in IE.

    If you browse to http://localhost IE doesn't take cookies, but Firefox does.
    So, you can browse to http//127.0.0.1 and IE (and FF) will take cookies and it works in both IE and FF.

    Other than that, if you upload the code to a real website (after you've fixed the shortcut script tags) it will work like the demo site.

IMN logo majestic logo threadwatch logo seochat tools logo