O'Reilly Forums: My Captcha Doesn't Work On P.623 - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

My Captcha Doesn't Work On P.623 Problem with the header

#1 User is offline   solid_mercury 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 07-January 13

Posted 07 January 2013 - 05:15 AM

Hi.
Actually I typed all the code in my editor and either copied the specified font in the server folder. I had no issue with sha or session lines. But when I try to run the script in the browser, it shows nothing except a missing image place holder. I tried various browsers but it didn't work either. :(
So I tried to make some changes in original code so the "imagepng" function create a file from image inside memory and disabled the header line of code; It works!
Here is my new code:
********************************************************************************************
<?php
session_start();
// Set some important CAPTCHA constants
define('CAPTCHA_NUMCHARS', 6); // number of characters in pass-phrase
define('CAPTCHA_WIDTH', 150); // width of image
define('CAPTCHA_HEIGHT', 50); // height of image
// Generate the random pass-phrase
$pass_phrase = "";
for ($i = 0; $i < CAPTCHA_NUMCHARS; $i++) {
$pass_phrase .= chr(rand(97, 122));
}
// Store the encrypted pass-phrase in a session variable
$_SESSION['pass_phrase'] = sha1($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, 30, 0, 5, CAPTCHA_HEIGHT - 7, $text_color, "Courier New Bold.ttf", $pass_phrase);
// Output the image as a PNG using a header

//header("Content-type: image/png");
imagepng($img, "captcha.png");
echo '<img src="captcha.png" />';

// Clean up
imagedestroy($img);
 ?>
*************************************************************************

But I want the script to be self-contained and could be called in the future lessons of book.
I think there is something wrong whit the header. Anybody can help me?

Thank you
0

#2 User is offline   drewdin 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 589
  • Joined: 11-February 10
  • Gender:Male
  • Location:Boston

Posted 08 January 2013 - 02:31 PM

View Postsolid_mercury, on 07 January 2013 - 05:15 AM, said:

Hi.
Actually I typed all the code in my editor and either copied the specified font in the server folder. I had no issue with sha or session lines. But when I try to run the script in the browser, it shows nothing except a missing image place holder. I tried various browsers but it didn't work either. :(
So I tried to make some changes in original code so the "imagepng" function create a file from image inside memory and disabled the header line of code; It works!
Here is my new code:
********************************************************************************************
<?php
session_start();
// Set some important CAPTCHA constants
define('CAPTCHA_NUMCHARS', 6); // number of characters in pass-phrase
define('CAPTCHA_WIDTH', 150); // width of image
define('CAPTCHA_HEIGHT', 50); // height of image
// Generate the random pass-phrase
$pass_phrase = "";
for ($i = 0; $i < CAPTCHA_NUMCHARS; $i++) {
$pass_phrase .= chr(rand(97, 122));
}
// Store the encrypted pass-phrase in a session variable
$_SESSION['pass_phrase'] = sha1($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, 30, 0, 5, CAPTCHA_HEIGHT - 7, $text_color, "Courier New Bold.ttf", $pass_phrase);
// Output the image as a PNG using a header

//header("Content-type: image/png");
imagepng($img, "captcha.png");
echo '<img src="captcha.png" />';

// Clean up
imagedestroy($img);
 ?>
*************************************************************************

But I want the script to be self-contained and could be called in the future lessons of book.
I think there is something wrong whit the header. Anybody can help me?

Thank you



Sounds like there is an issue with your image paths, I don't see where you have one listed so I bet its not sure where to put it and take it from.
0

#3 User is offline   solid_mercury 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 07-January 13

Posted 11 January 2013 - 12:38 PM

Thank you for replying.

I figured it out the day I posted the problem.
It was the bad with my editor that was saving my code in UTF encoding character-set. I think it causes the header part of code to not working.
I changed it to ANSI and it worked.

Pardon me for sayin it late.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users