Jump to content

Captcha Help


mrzebra81

Recommended Posts

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???

Link to comment
Share on other sites

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`‚

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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`‚

 

 

Link to comment
Share on other sites

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.

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.