Page 1 of 1

How to implement refresh (re-draw) into Kcaptcha

Posted: Thu Jul 02, 2009 9:40 am
by wayneob
Hi guys,

Been trying to get my head around this for several days now with no avail :banghead:

I currently use Kcaptcha - http://www.captcha.ru/en/kcaptcha/ in my forms but there is no refresh option like on some other scripts which I would really like to implement in. Some other scripts that i have seen use javascript or ajax to make this possible but I dont want to start over with another captcha but merley upgrade this one.

The jscript used in 1 i have is:

Code: Select all

<script type="text/javascript">
  function reloadCaptcha(imageName)
  {
    var randomnumber=Math.floor(Math.random()*1001); // generate a random number to add to image url to prevent caching
    document.images[imageName].src = document.images[imageName].src + '&rand=' + randomnumber; // change image src to the same url but with the random number on the end
  }
  </script>
which is triggered from this:

Code: Select all

<img src="captcha.php?hash=<?php echo $hash; ?>" alt="captcha" name="captchaImage"><br>
<a href="#" onClick="reloadCaptcha('captchaImage'); return false;">refresh image</a>
and works with this php file:

Code: Select all

<?php
// Create our image
$image = imagecreatetruecolor(100, 30);
 
// Assign a background colour. I have set it to white but background is going to be transparent anyway
$background = imagecolorallocate($image, 0,0,0);
 
// Make background transparent by setting the colour we are going to use as transparent
imagecolortransparent($image, $background);
 
// Fill it in with the background colour. This is required to make the background transparent
imagefilledrectangle($image, 0, 0, 199, 49, $background);
 
// function to generate captcha characters
function captchaChars($hash)
{
  //  Generate a 32 character string by getting the MD5 hash of the servers name with the hash added to the end.
  //  Adding the servers name means outside sources cannot just work out the characters from the hash
  $captchastr = md5($_SERVER['SERVER_NAME'] . $hash);
  return strtoupper($captchastr); // Make all our characters uppercase for clarity in the image
}
 
// Lets get the characters to show in the image or say 'error' if no hash submitted
$str = (!empty($_GET['hash'])) ? captchaChars($_GET['hash']) : 'ERROR';
 
// Assign a colour for the text and lines. I've chosen a shade of grey
$our_colour = imagecolorallocate($image, 140,140,140);
 
// Lets add three random background lines
for ($i = mt_rand(5, 8); $i <= 29; $i += 10)
{
  imageline($image, mt_rand(0, 100), $i + mt_rand(-5, 5), mt_rand(0, 100), $i + mt_rand(-5, 5), $our_colour);
}
 
// Set a random horizontal starting position
$x_pos = mt_rand(10, 20);
 
// Lets loop through our string adding one character at a time in a randomish position
// We start with the first character and then use every seventh character just for added randomness
for($i = 0; $i <= 28; $i += 7)
{
  imagestring($image, 5, $x_pos, mt_rand(0, 12), $str[$i], $our_colour); // add a character from our string
  $x_pos += mt_rand(10, 18);  // Move the horizontal position by a random amount
}
 
// Add some wavy distortion to the image
$wave = rand(3,5);
$wave_width = rand(8,15);
for ($i = 0; $i < 200; $i += 2)
{
  imagecopy($image, $image, $i - 2, sin($i / $wave_width) * $wave, $i, 0, 2, 40);
} 
 
// Send the gif header. We use a gif because of IE6's poor support for PNG transparency
header('Content-type: image/gif');
// Dump the image
imagegif($image);
 
?>
I would ideally like to get this to work in Kcaptcha but seems to be a little over my head so far :?

In my form page i have this to call the Captcha:

Code: Select all

<img src="../mail/captcha/index.php?<?php echo session_name()?>=<?php echo session_id()?>"
and i was trying to put this into a php include and to refresh the php include without refreshing page, beacuse when you refresh the page it looses the data the browser put in the form, but this failed.

So basically the need for this refresh is so if the browser completes the form and code is unreadable for them rather than submit and it be wrong they get choice to refresh to submit another.

When you complete the form and is wrong code you click to go back and another code is supplied and data user inputs is still there so this must be relatively easy to implement as it uses session_start(); in from results page and i have session_name(); in form page so data is still there.

Hopefully this is enough info for some of you to work out what i need to do for now, apologies if confusing for some :lol:

any help or direction would be appreciated :wink: