PHP session problem— captcha/Joomla

bingowriter

New Member
I have a Joomla component which calls a helper function to create a captcha image. Everything works fine when sh404 is disabled, but when sh404 is enabled the session variable for the security image isn't being set correctly so when you submit the form you get 'Invalid Captcha' message. The funny thing is if you submit another 5-6 times, it validates fine and submits. I've tried just about everything I can think of - when I echo the session variable and submitted captcha code it appears that the session is a step behind - for example:If I submit the form the first time and echo the session variable and submitted code, it looks like the session variable is not being set in time - I get a blank value for the session variable. Then I submit the form again and the session variable is the value of the previous captcha image. Here's the code that generates and validates the captcha. Thanks!\[code\]//Generate Captcha image linkfunction Captchalink($capid = ''){ return 'index.php?option=com_mycomponent&view=home&task=newCaptcha&capid='.$capid;}function generateCode($characters) { /* list all possible characters, similar looking characters and vowels have been removed */ $possible = '23456789bcdfghjkmnpqrstvwxyz'; $code = ''; $i = 0; while ($i < $characters) { $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1); $i++; } return $code;}function CaptchaSecurityImages($capid = '') { $font = dirname(__FILE__).DS."monofont.ttf"; $width = 90; $height = 30; $characters = 6; $session =& JFactory::getSession(); //Clean buffers while (ob_get_level()) { ob_end_clean(); } // start output buffering if (ob_get_length() === false) { ob_start(); } $code = mycomponentHTML::generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colors */ $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); /* generate random dots in background */ for( $i=0; $i<($width*$height)/3; $i++ ) { imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color); } /* generate random lines in background */ for( $i=0; $i<($width*$height)/150; $i++ ) { imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4])/2; $y = ($height - $textbox[5])/2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); /* set session variable for newly created code */ $session->set('security_code_'.$capid, md5($code)); ob_end_flush(); die();}function CaptchaValidate($capid = ''){ $session =& JFactory::getSession(); if( $session->get('security_code_'.$capid) == md5(JRequest::getVar('security_code_'.$capid)) ) { $session->clear('security_code_'.$capid); return true; }else{ return false; }}\[/code\]
 
Back
Top