PHP Simple CAPTCHA

A simple yet effective captcha PHP class.

$captcha = new Captcha();
echo $captcha->generate();


Live example according to your IP:

class Captcha{
/**
 * @author Webarto.com
 * @copyright 2010
 */
 
    var $multiplier = 31; //Set multiplication number, bigger number, more chars.
    
    public function build(){
        $ip = explode(".", $_SERVER["REMOTE_ADDR"]);
        return strtoupper(dechex($ip[2].$ip[3] * $this->multiplier)); //Do not touch. Fragile.
    }
    
    public function check($input){ //Checks if entered captcha is valid
        if($input == $this->build()){
            return true;
        }else{
            return false;
        }
    }

    public function generate(){ //Generate image, use in separate file, headers sent.
        $string = $this->build();
        $im = @imagecreate(strlen($string) * 8, 16); //Width based on length
        $backgroundColor = imagecolorallocate($im, 255, 255, 255);
        $textColor = imagecolorallocate($im, 33, 33, 33);
        imagestring($im, 4, 0, 0, $string, $textColor);
        header("Content-type: image/png");
        imagepng($im);
        imagedestroy($im);
    }
}
©2009-2011 Webarto • web design & development • Tuzla // Sarajevo // Beograd