mrzebra81 Posted July 24, 2011 Share Posted July 24, 2011 Hi, I'm trying to get a math captcha image to display on localhost using WAMP, but all i get is a broken image box. Gd is enabled. Under apache, modules, I have php5_module selected. Here is my code: <?php $num1 = rand(1, 20); $num2 = rand(1, 20); $value = $num1. " + " .$num2. " = "; $ans=$num1+$num2; $_SESSION['check'] = $ans; // image properties $height=30; $width=100; $image = imagecreatetruecolor($width, $height); $background = imagecolorallocate($image, 29, 29, 29); $text = imagecolorallocate($image, 153, 153, 153); $line = imagecolorallocate($image, 58, 58, 58); imagefill($image, 0, 0, $text); imageline($image, 0, $height/2, $width, $height/2, $line); imagestring($image, 1, 5, 1, $value, $text); header("Content-type: image/png"); imagepng($image); imagedestroy($image); ?> Then i have <img src=captcha.php> where i want the image in my form to be displayed. I have no idea why it doesn't want to work....any suggestions??? Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/ Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 Try removing the header information and see if it gives you an error. Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246457 Share on other sites More sharing options...
mrzebra81 Posted July 25, 2011 Author Share Posted July 25, 2011 I still get a broken image on the page but if i just view the captcha.php page it displays this: ‰PNG IHDRdU9,¤kIDAThíÓÁ €0A«J3©+ÝÚ‚û ™ Ž…»×Zÿ<»œD¬@¬@¬@¬@¬@¬@¬@¬@¬@¬@¬@¬@¬àcìÞpŒwι{Ã1Ü0++++++++++++øáðÓÏ÷IEND®B`‚ Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246459 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 Ok, put the header information back and when you load the file, do it like this: <img src="captcha.php?time=<?php echo time();?>"> and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246461 Share on other sites More sharing options...
mrzebra81 Posted July 25, 2011 Author Share Posted July 25, 2011 So I have this on my addComment.php page: <?php // form to add comment echo "<table border=0 width=93%> <tr> <td align=center><br><br><h2>Post A Comment</h2><br><br> </td> </tr> <tr> <td align=center> <form action='guestProcess.php' method='post'><font color=#999999> <p>What is your name? <br><input type=text width=100 class=guestBox name='name'></p> <p>Where are you from? <br> <input type=text class=guestBox name='city'></p> <p>Please enter your comments:<br> <textarea class=guestBox name='comment' style='padding: 4px; overflow:hidden; border-style: single; border-color: #999999; height:100px;'></textarea></p> <p><img src='captcha.php'> <input type='text' class=cap size=1 width=3 maxlength=3 onKeyPress='return check_qty(event);' name='check'> <br> <p><input type='submit' class='updateButton' name='submit' value='POST COMMENT'></p> </font></form> </td> </tr> </table>"; ?> When i change the <img src='captcha.php'> to what you suggested nothing happens...it is just displayed as a broken image box. Maybe I'm doing something wrong, I'm not sure. hahaha Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246470 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 Ok, I think you either have a configuration error or you are just being a ding dong. Here is what I would do. 1) Ensure that you are indeed using the right path to the file. 2) Load the file directly in your browser. Try IE, Chrome, and Firefox and see if anything different happens. 3) Add comments to the header declaration and enable php error reporting. Reload the page, look for errors. 4) If this fails, upload it to a server and see if it works on the server. If it works on the server, you have a configuration error. (Your script works on my computer.) Like I said before this site crashed though, you have the color for background set to your font color. Make sure you update it to $background or you won't be able to see anything when it does work. Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246472 Share on other sites More sharing options...
gizmola Posted July 25, 2011 Share Posted July 25, 2011 That code works fine for me, although you do have an issue with colors. This is working code on my server: $num1 = rand(1, 20); $num2 = rand(1, 20); $value = $num1. " + " .$num2. " = "; $ans=$num1+$num2; $_SESSION['check'] = $ans; // image properties $height=30; $width=100; $image = imagecreatetruecolor($width, $height); $background = imagecolorallocate($image, 29, 29, 29); $text = imagecolorallocate($image, 255, 255, 255); $line = imagecolorallocate($image, 58, 58, 58); imagefill($image, 0, 0, $background); imageline($image, 0, $height/2, $width, $height/2, $line); imagestring($image, 1, 5, 1, $value, $text); header("Content-type: image/png"); imagepng($image); imagedestroy($image) Notice i intentionally omitted the closing "?>". That prevents any extraneous whitespace from being output, that could be causing issues. Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246475 Share on other sites More sharing options...
mrzebra81 Posted July 25, 2011 Author Share Posted July 25, 2011 so i uploaded the captcha.php file to my website...in explorer it displays a broken image and in firefox it displays "the image http://www.coppercreekbathandbody.ca/captcha.php cannot be displayed because it contains errors. I also uploaded the addComment.php page to see how it displays...in IE, it's a broken image and in firefox there is no image displayed. I'm also using WAMP to test before I upload to my site...could it be something in WAMP that is giving me grief? Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246493 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 On the live server, remove the header info //header("Content-type: image/png"); Then, add to the top: error_reporting(E_ALL); ini_set('display_errors', '1') Finally, upload a copy of that file with a .txt extension. See what happens. Let me know when you do this, I'd like to take a look at that as well as the txt. Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246497 Share on other sites More sharing options...
gizmola Posted July 25, 2011 Share Posted July 25, 2011 Did you copy my code exactly? That code does create an image, as I stated, so I've verified it works. If it is not working in your environment it's a configuration problem. Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246498 Share on other sites More sharing options...
mrzebra81 Posted July 25, 2011 Author Share Posted July 25, 2011 Yeah, I copied it exactly. Uploading is done...just displays: ‰PNG IHDRdU9,¤ÄIDAThíÖK à FáZ²*7ãFÞM¸Û„P⣞t`.ü߬=ÜBB)å%kÞ»àI?–™Mö¬?5³ùb_Žö«®w)•s¾ýSÔ™¬É ÏIL»w”Þ¾€ónÕ™¬‰Úb}dêÊîzCÇbQó!]Yö(¡}u8¯qã—½õ£—?ubɈ޳ŠP,@±ÅBŒq÷Ü8RJ»Ïà†þ†€bŠ( X€bŠ( X€bŠ( XÀt^?q±û±´IEND®B`‚ Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246503 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 When you are creating the file, there isn't a blank line at the beginning is there? Make sure there is absolutely no whitespace characters outside of php. Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246505 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 Oh... Session start. You cant send headers twice. Edit: ^^ Should still work. If you have whitespace above it, it won't. Which it looks like you have whitespace. Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246506 Share on other sites More sharing options...
mrzebra81 Posted July 25, 2011 Author Share Posted July 25, 2011 Yes, the blank space fixed it and it now displays an image. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246507 Share on other sites More sharing options...
gizmola Posted July 25, 2011 Share Posted July 25, 2011 You didn't follow the instructions I provided, when I said not to include the ending php tag for the script did you? Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246527 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 Gizmola, no. He had a newline before php even started. Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246531 Share on other sites More sharing options...
mrzebra81 Posted July 25, 2011 Author Share Posted July 25, 2011 yes, i had a new line before the php started. I never knew a blank line could cause that many problems. Hahaha. Thanks you both for all the help. Greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246547 Share on other sites More sharing options...
gizmola Posted July 25, 2011 Share Posted July 25, 2011 it doesn't matter if you have a newline or not, if you don't include the end tag. At any rate, it doesn't matter, so long as it's sorted now. Yes you have to be careful about whitespace with php scripts especially where header() calls are concerned. Quote Link to comment https://forums.phpfreaks.com/topic/242686-captcha-help/#findComment-1246553 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.