Problem with my captcha script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Hieroglyphics
Forum Newbie
Posts: 13
Joined: Wed Jan 20, 2010 8:25 pm

Problem with my captcha script

Post by Hieroglyphics »

captcha.php:

Code: Select all

<?php
$rand = $Raen->createRand(5);
$session = md5($rand);
$Raen->createSession('captcha_session', $session);
 
$Raen->getObject('img')->createImage(122, 30);
 
$Raen->getObject('img')->imageColor(235, 235, 235);
$text = $Raen->getObject('img')->imageColor(42, 151, 216);
 
$Raen->getObject('img')->imageString(5, 5, 8, 'Raen_'.$rand, $text);
 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-type: image/png');
 
$Raen->getObject('img')->show();
?>

Code: Select all

class Raen_Image  {
    private $image;
    
    public function createImage($width, $height) {
        $this->image = imagecreate($width, $height);
    }
    
    public function imageColor($r, $g, $b) {
        return imagecolorallocate($this->image, $r, $g, $b);
    }
    
    public function imageString($font, $x, $y, $string, $color) {
        imagestring($this->image, $font, $x, $y, $string, $color);
    }
    
    public function show() {
        imagepng($this->image);
        imagedestroy($this->image);
    }
}

Code: Select all

 
public function createSession($name, $data) {
        $_SESSION[$name] = $data;
    }
    
    public function getSession($name) {
        return $_SESSION[$name];
    }
    
    public function createRand($digits) {
        $min = "1";
        $max = "9";
        $i=1;
        while($i < $digits) {
            $min .= "0";
            $i++;
        }
        $a=1;
        while($a < $digits) {
            $max .= "9";
            $a++;
        }
        $rand = rand($min, $max);
        return $rand;
    }
 
PHP script to control ajax:

Code: Select all

session_start();
require_once('../../inc.php');
$captchaInt = (int) $_GET['captcha'];
$Captcha = md5($captchaInt);
 
if ($Captcha != $Raen->getSession('captcha_session')) {
    echo "Your verification string is incorrect.<br />";
    echo $Raen->getSession('captcha_session');
    echo "<br />$Captcha";
} else if ($Captcha == NULL) {
    echo "Please enter the verification string.";
} else {
    echo $Raen->getObject('reg')->successImage();
}

The problem is, sometimes it says the captcha is wrong even though it's right 100% :( help please :/
Hieroglyphics
Forum Newbie
Posts: 13
Joined: Wed Jan 20, 2010 8:25 pm

Re: Problem with my captcha script

Post by Hieroglyphics »

bump
Post Reply