Jump to content

Creating a CAPTCHA image


genzedu777

Recommended Posts

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

?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.