Jump to content

CAPTCHA random number not working


crawdevil

Recommended Posts

Hi All

 

Please help! I'm a PHP newb tring to build a CAPTCHA for my website I have cobled together the following code from various tutorials and examples but my random number won't write to the image.

 

I have posted the code below everying works fine if I replace $this->captchaString with plain text instead of the variable in imagestring($im, 10, 50, 50, $this->captchaString, $text_color);

 

But then it defeats the whole purpose.

 

<?php

  class Captcha

  {

    var $captchaSize;

    var $captchaString;

 

    function Captcha()

    {

header("Content-type: image/png");

$this->captchaSize = 6;

$this->stringGen();

    $this->makeCaptcha();

}

 

function stringGen ()

    {

      $uppercase  = range('A', 'Z');

      $lowercase  = range('a', 'z');

      $numeric    = range(0, 9);

      $chars = array_merge($uppercase, $lowercase, $numeric);

      $charsLength = count($chars) - 1;

  for ($i = 0; $i < $this->chaptchaSize; $i++)

      {

      $this->captchaString .= (string)$chars[mt_rand(0, $charsLength)];

      }

    }

 

function makeCaptcha(){

$imagelength = $this->captchaSize * 25 + 16;

      $imageheight = 75;

$im = @imagecreate($imagelength, $imageheight)

    or die("Cannot Initialize new GD image stream");

$background_color = imagecolorallocate($im, 255, 255, 255);

$text_color = imagecolorallocate($im, 233, 14, 91);

imagestring($im, 10, 50, 50, $this->captchaString, $text_color);

imagepng($im);

imagedestroy($im);

}

function getCaptchaStr()

    {

      return $this->captchaString;

    }

}

?>

 

Thanks for any help you can give

 

Clive

Link to comment
https://forums.phpfreaks.com/topic/89611-captcha-random-number-not-working/
Share on other sites

Have you tested stringGen()? Can't tell off hand if its right or not.

 

I just grabbed this code quick off a tutorial. It might work easier. 

 

//Let's generate a totally random string using md5
$md5_hash = md5(rand(0,999)); 
//We don't need a 32 character long string so we trim it down to 5 
$security_code = substr($md5_hash, 15, 5); 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.