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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.