0

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)
Click Here

exmple.php

<?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
//
//   CF Captcha v0.9
//   -------------------------------
//
//   Author:    codefuture.co.uk
//   Version:   0.9
//   Date:      28-Apr-10
//
//   download the latest version from - http://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', 5 );

// 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 ( strtolower( substr($domain, 0, 4) ) == 'www.' )
        $domain = substr($domain, 4);
    if ( substr($domain, 0, 1) != '.' )
        $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(130, 35);
        $rgb = $this->html2rgb($this->bg_color);
        $bgcolor = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
        imagefill($im, 0, 0, $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($im, rand(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 != 3 ? rand(10,99) : rand(1,9);
        $number2= rand(1,9);
        imagettftext($image, $this->rand_fontsize(), $this->rand_angle(), $x, $this->rand_Y(), $this->rand_color($image), $this->font(),($sum ==3?'':substr($number1, 0,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 ( strtolower( substr($domain, 0, 4) ) == 'www.' )
            $domain = substr($domain, 4);   // Fix the domain to accept domains with and without 'www.'. 
        if ( substr($domain, 0, 1) != '.' )
            $domain = '.'.$domain;  // Add the dot prefix to ensure compatibility with subdomains
        setcookie('Captcha', md5(SALTING.$this->UserString.$_SERVER['REMOTE_ADDR'].$time).'.'.$time, null, '/',$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($color, 1);

        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);
    }
}

?>
4
Contributors
9
Replies
44
Views
4 Years
Discussion Span
Last Post by dansonyun
0

This tiny captcha code is kind of weird. Only works in Firefox but not IE (6,8)

There's nothing wrong with the code.

I think might have to do with your IE browsers.

I test it on IE10 so there's nothing wrong it.

I would consisted this as a IE version issue.

You can used CSS code to work around with this.

But I don't think there's PHP code work around with this.

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.

0

But when I download the code on local machine. It works only in Firefox.

It works fine on my computer.

If the Author's website works both on FF and IE online which it works and I also ran the code and it works on my IE and firefox too. Then it has nothing to do with the code.

Then it has to do with the setting on your browser/computer.

0

Just tried on my neighbor's computer.
Both two computer's works fine in Firefox but shows a cookie error on IE.
I tried almost every possible setting in IE. Turn off all anti-virus and anti-spam program. Turn the security level of IE to lowest. Accept all cookies....

Neighbor's computer ;
Win7
Apache  2.215
PHP     5.32
MySQL   5.07


My Computer ; 
WinXP
Apache  2.063
PHP     5.214
MySQL   5.09
0

Do you really need it for IE6? That's a lot of shiving and poncing about for what may be very little return. The support for IE8 should be considerably better.

I have IE10 and if you go into Developer Tools (F12), you can set the version of IE that you need to check against (IE v7-10). They all work fine on the site in your link.

The script does require GD library to be installed with PHP - this is usually the default state for newer versions of PHP, but you should check via phpinfo() just in case. However, if GD is not installed, then it wouldn't work with FF either.

//EDIT

just thought, make sure your code isn't tripping quirksmode. SHouldn't have thought it would matter, but I'm not delving into a thrid-party script.

Edited by diafol

0

Thanks for bradgrafelman!!!

WTH? Where is the info for this? I can't see any reference to http://localhost/JIM/002/Example.php in this thread. Seems like cross-posting to me. Did you get this info from another thread, forum, site? If so, kindly provide the link so that others with a similar problem may find background info pertaining to the solution. Oh, and then you can press the 'Mark Question Solved' button at the bottom of the screen. :)

Edited by diafol

0

"Localhost" and "127.0.0.1" is always the same thing to me. But I remember some articles I read few weeks ago to declare some new product or new services are not recognize "Localhost" anymore.
"bradgrafelman" reminds me the problem might caused by url.
Whenever you download this cute captcha from Author's site.
CF Captcha v0.9
// Author: codefuture.co.uk
It works fine no matter what with Firefox.

But if you test this captcha with IE8. You'll get the error message if your url like this:
Localhost/something/something.php
There's something inside the code has conflicted with the keyword "Localhost" .
The error message gone simply just replace your url in your local machine like this :
127.0.0.1/something/something.php

So far , I just know there's some of the code inside this captcha has conflicted with keyword "Localhost" along with IE 6 & 8. But I'm not able to pin-point it so far.
I would be very glad if anyone might able to dig deeper in this issue.

So.... hum.... Just remember this. Localhost is no longer the same as 127.0.0.1 .

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.