By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

Problem with captcha image not showing

100+
P: 290
Hi,
I have a captcha script which should pick up a background image and add some random letters to it and re-display

This is the part of the form that the captcha image is part of:


Expand|Select|Wrap|Line Numbers
  1. <span >Verification Image:</span>
  2. <span ><img src="captcha.php" id="captcha" />
  3. <a href="#renew" onclick="javascript: document.getElementById('captcha').src = 'captcha.php?' + Math.random();">refresh</a> </span>
  4.  
  5. <span >Enter 8 character key:</span>
  6. <span ><input type="text" name="imgver" id="imgver" onkeyup ="this.value=this.value.toUpperCase()"></span>
  7.  
  8. <input class="button2" type="submit" value="Give Me Free Entry Now"/> 
  9.  
And here is the captcha script that is called:

Expand|Select|Wrap|Line Numbers
  1. #since we are storing our data using Sessions, we need to start a session
  2. session_start();
  3. #$bg_image is the image that will be used for the background of our captcha
  4. #you will have to replace the value with your bg image.
  5. $bg_image = "images/cap02.png";
  6. #we're going to put some lines throughout the image to make it a bit harder for bots to crack
  7. #to color the lines, we need to fill in the color fields using RGB values (0-255 for each color)
  8. $line_color = array(
  9. "R" => 150,
  10. "G" => 150,
  11. "B" => 150
  12. );
  13. #set the number of line to display in our captcha
  14. $numLines = 5;
  15. #set the length of the key to display in our captcha
  16. $keyLength = 6;
  17. #set the color of the text in our captcha
  18. $textcolor = array(
  19. "R" => 0,
  20. "G" => 255,
  21. "B" => 0
  22. );
  23.  
  24. #get some file attribures of our bg image, all we are going to use is witdth and height.
  25. list($width, $height, $type, $attr) = getimagesize($bg_image);
  26.  
  27. #using PHP's GD Library, we're going to create our base captcha, which starts with our BG image.
  28. $captcha = imagecreatefrompng($bg_image);
  29. #sets the color for our key, the color was defined above.
  30. $keycol = imagecolorallocate($captcha, $textcolor["R"],$textcolor["G"],$textcolor["B"]);
  31. #start a loop to add our lines to our captcha
  32. for($i = 0; $i < $numLines; $i++)
  33. {
  34.     $line = imagecolorallocate($captcha,$line_color["R"],$line_color["G"],$line_color["B"]);
  35.     imageline($captcha,rand(0, $width),rand(0,$height),rand(0, $width),rand(0,$height),$line);
  36. }
  37. #generate our random key
  38. $string = GenKey($keyLength);
  39. #add our random key to our captcha
  40. imagestring($captcha, 9, rand(1, 30), rand(1, 15), $string, $keycol);
  41. #encrypt our key and add it to our session data.
  42. $_SESSION['key'] = md5($string);
  43. #send HTTP header to tell client we're going to display an image.
  44. header("Content-type: image/png");
  45. #dsplay image
  46. imagepng($captcha);
  47.  
  48. function GenKey ($length)
  49. {
  50.     #define the letter / number that will be used in our key.
  51.     $chars = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
  52.     #start a loop to make the key.
  53.     for($i = 0; $i < $length; $i++)
  54.     {
  55.         #pick a random start place in the string
  56.         $rand_start = rand(1, strlen($chars) - 1);
  57.         #add this character to our key
  58.         $key .= substr($chars, $rand_start, 1);
  59.     }
  60.     #return our key
  61.     return $key;
  62. }
  63. ?> 
I have a background image that is here:


image


As far as I can see, everything should work but I get nothing displayed at all.

Can anyone spot my errors ?
Thanks for helping.
Jan 14 '09 #1
Share this Question
Share on Google+
10 Replies


Atli
Expert 5K+
P: 5,058
Hi.

The captcha.php script is probably printing some errors. Try commenting out the header and navigate to the capatcha.php file in your browser. See if there are any errors.
Jan 14 '09 #2

100+
P: 290
Thanks for your reply, but I am not sure what you mean or how to do it :(
Jan 14 '09 #3

Atli
Expert 5K+
P: 5,058
Line #44 in the code you posted. Remove it.
You can simply put # or // in front of it to comment it out.

Then open the script in your browser.
http://www.devchoice.info/captcha.php

That should tell you why your image isn't showing like it should.
Jan 14 '09 #4

100+
P: 290
OK ,
thanks for explaining that.

It shows an error:
Notice: Undefined variable: key in /home/chosy/public_html/captcha.php on line 64

That line 64 refers to:

$key .= substr($chars, $rand_start, 1);

So its missing the $key variable,
Am I supposed to suply that variable somehow ?
Jan 14 '09 #5

Markus
Expert 5K+
P: 6,050
@jeddiki
Because you haven't defined the variabled, you can't add to it using '.='

Add $key = ""; to your GenKey method (to the top of it).
Jan 14 '09 #6

100+
P: 290
Thanks Marcus,

I have dropped it in here: (line 14 - 15 )

Expand|Select|Wrap|Line Numbers
  1. #since we are storing our data using Sessions, we need to start a session
  2. session_start();
  3. #$bg_image is the image that will be used for the background of our captcha
  4. #you will have to replace the value with your bg image.
  5. $bg_image = "images/cap02.png";
  6. #we're going to put some lines throughout the image to make it a bit harder for bots to crack
  7. #to color the lines, we need to fill in the color fields using RGB values (0-255 for each color)
  8. $line_color = array(
  9. "R" => 150,
  10. "G" => 150,
  11. "B" => 150
  12. );
  13.  
  14. #initialise the key
  15. $key = "";
  16.  
  17. #set the number of line to display in our captcha
  18. $numLines = 5;
  19.  
  20. #set the length of the key to display in our captcha
  21. $keyLength = 6;
  22.  
  23. #set the color of the text in our captcha
  24. $textcolor = array(
  25. "R" => 0,
  26. "G" => 255,
  27. "B" => 0
  28. );
  29.  
Is that what you meant ?
Jan 14 '09 #7

Markus
Expert 5K+
P: 6,050
Nope, I meant inside your GenKey method, like so:

Expand|Select|Wrap|Line Numbers
  1. function GenKey ($length)
  2. {
  3.     # key
  4.     $key = "";
  5.     #define the letter / number that will be used in our key.
  6.     $chars = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
  7.     #start a loop to make the key.
  8.     for($i = 0; $i < $length; $i++)
  9.     {
  10.         #pick a random start place in the string
  11.         $rand_start = rand(1, strlen($chars) - 1);
  12.         #add this character to our key
  13.         $key .= substr($chars, $rand_start, 1);
  14.     }
  15.     #return our key
  16.     return $key;
  17. }
  18.  
Does that work now?
Jan 14 '09 #8

100+
P: 290
Ah ha
Good job I asked ;)

When I run it I get this:

�PNG  ��� IHDR���x������h�M����IDATh���y��U} ����o��{�-��4 M�(��R���N,���%1�X��lj����2�3��m�$�)*��Xf*(� �ұ��n�_�~�}w�mg�?^�hh�(Ƅ�n������o�{~�s.:�7xYo�W ��ڛsZ�O��j��>��"��6"�ou��+�ke��s�s��4�m����S ��H�`n�$���"�2ވ���k���x�<�d�<(�ה��3�S����Ѧ��`kk }0����-V���#��S��O���̨rd�mq���S�4W5�����!��� 51C�M=�&��'7c�оC����L{��3�

so maybe thats working

I'll take out the comment out at:
#header("Content-type: image/png");

and run it from the form.

Thanks
Jan 14 '09 #9

100+
P: 290
Yep, That did the job !

I guess I must have deleted that variable somehow !

Thanks again for your help.
Jan 14 '09 #10

Markus
Expert 5K+
P: 6,050
Glad you got it working.

Markus.
Jan 14 '09 #11

Post your reply

Sign in to post your reply or Sign up for a free account.