genzedu777 Posted July 25, 2010 Share Posted July 25, 2010 Hi, I need help in my code. I have written a CAPTCHA code, but it is not appearing when I test run in my website. My code is below, could anyone highlight to me my error? Any problems to those that were highlighted in red? Thanks <?php session_start(); // Set some important CAPTCHA constants define('CAPTCHA_NUMCHARS', 6); // number of characters in pass-phrase define('CAPTCHA_WIDTH', 100); // width of image define('CAPTCHA_HEIGHT', 25); // height of image // Generate the random pass-phrase $pass_phrase = ""; for ($i = 0; $i < CAPTCHA_NUMCHARS; $i++) { $pass_phrase .= chr(rand(97, 122)); //chr(), convert a number to its ASCII character equivalent } // Store the encrypted pass-phrase in a session variable $_SESSION['pass_phrase'] = SHA($pass_phrase); // Create the image $img = imagecreatetruecolor(CAPTCHA_WIDTH, CAPTCHA_HEIGHT); // Set a white background with black text and gray graphics $bg_color = imagecolorallocate($img, 255, 255, 255); // white $text_color = imagecolorallocate($img, 0, 0, 0); // black $graphic_color = imagecolorallocate($img, 64, 64, 64); // dark gray // Fill the background imagefilledrectangle($img, 0, 0, CAPTCHA_WIDTH, CAPTCHA_HEIGHT, $bg_color); // Draw some random lines for ($i = 0; $i < 5; $i++) { imageline($img, 0, rand() % CAPTCHA_HEIGHT, CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphic_color); } // Sprinkle in some random dots for ($i = 0; $i < 50; $i++) { imagesetpixel($img, rand() % CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphic_color); } // Draw the pass-phrase string imagettftext($img, 18, 0, 5, CAPTCHA_HEIGHT - 5, $text_color, 'Courier New Bold.ttf', $pass_phrase); // Output the image as a PNG using a header header("Content-type: image/png"); imagepng($img); // Clean up imagedestroy($img); ?> Quote Link to comment https://forums.phpfreaks.com/topic/208843-creating-a-captcha-image/ Share on other sites More sharing options...
wildteen88 Posted July 25, 2010 Share Posted July 25, 2010 On line 16 you're calling a function called SHA(), this function does not exist within your script. $_SESSION['pass_phrase'] = SHA($pass_phrase); I think you meant to call sha1 On line 40 make sure the font file (Courier New Bold.ttf) is within the same directory as your script. PHP does not search your computers font directory for fonts Quote Link to comment https://forums.phpfreaks.com/topic/208843-creating-a-captcha-image/#findComment-1090946 Share on other sites More sharing options...
genzedu777 Posted July 27, 2010 Author Share Posted July 27, 2010 Hi Wildteen88, Thank you so much for the advice. Could I ask you for another advice. May I know what is the difference between SHA and SHA1? I understand that SHA encrypt text, however how do we decrypt the text again? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/208843-creating-a-captcha-image/#findComment-1091691 Share on other sites More sharing options...
PFMaBiSmAd Posted July 27, 2010 Share Posted July 27, 2010 May I know what is the difference between SHA and SHA1? There is no SHA hash, but SHA would generally be referring to SHA1 - http://en.wikipedia.org/wiki/SHA-1 I understand that SHA encrypt text, however how do we decrypt the text again? You don't. SHA/SHA1 is not encryption. It is a one way hash (checksum) and it cannot be decrypted. Quote Link to comment https://forums.phpfreaks.com/topic/208843-creating-a-captcha-image/#findComment-1091695 Share on other sites More sharing options...
genzedu777 Posted July 28, 2010 Author Share Posted July 28, 2010 Hi PFMaBiSmAd, So what kind of command or function should I used if I will like to decrypt the entered password. As SHA1, would usually munch the password into 40 hexadecimal figures. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/208843-creating-a-captcha-image/#findComment-1091956 Share on other sites More sharing options...
BillyBoB Posted July 28, 2010 Share Posted July 28, 2010 You wouldn't need to decrypt it. Just redo the hash on the entered phrase and compare. $pass_phrase = sha1($_POST['pass_phrase']); if($pass_phrase==$_SESSION['pass_phrase']) echo 'Success'; else echo 'Failure'; Quote Link to comment https://forums.phpfreaks.com/topic/208843-creating-a-captcha-image/#findComment-1091997 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.