Jump to content

PHP Captcha Issue


ZHarvey

Recommended Posts

I am following a 4-year old tutorial on creating PHP captchas through the GD.  When I include the PHP-generating code on a page that has nothing else (no HTML/text content), the captcha loads perfectly fine.

 

But when I try to mix it in with any HTML at all, I get the error: "The image “http://testing.mysite.com/dts/cots/Test.php” cannot be displayed, because it contains errors.". Here is my code:

 

// Author: Andrew Walsh
// Original Post: http://www.codewalkers.com/c/a/Miscellaneous/Creating-a-CAPTCHA-with-PHP/
session_start();
$md5 = md5(microtime() * mktime());
$string = substr($md5,0,7);
$captcha = imagecreatefrompng("./img/captcha.PNG"); 
$black = imagecolorallocate($captcha, 0, 0, 0);
$line = imagecolorallocate($captcha,233,239,239); 
imageline($captcha,0,0,39,29,$line);
imageline($captcha,40,0,64,29,$line);
imagestring($captcha, 5, 20, 10, $string, $black); 
$_SESSION["CAPTCHA_CHALLENGE"] = md5($string);
header("Content-type: image/png");
imagepng($captcha);

 

The reason for the error is obvious: I am telling the system that I want to set the page's content type to image/png. If I am placing my CAPTCHA in an HTML form, then by definition there will already be text/html content sent back from the server by the time it gets to the header definition.  According to the PHP manual, header must be the first call in a PHP page, otherwise it will throw an error.

 

Is there a way to hack this or make this work so that I can place the code in an HTML form, mid-page? I guess I just don't know enough about the GD or PHP in general to come up with my own solution.

 

Thanks,

ZHarvey

Link to comment
https://forums.phpfreaks.com/topic/201780-php-captcha-issue/
Share on other sites

Thanks for the reply,

 

Here is my code:

<?php

session_start();
// Make captcha
$md5 = md5(microtime() * mktime());
$string = substr($md5,0,7);
$captcha = imagecreatefrompng("./img/captcha.PNG"); 
$black = imagecolorallocate($captcha, 0, 0, 0);
$line = imagecolorallocate($captcha,233,239,239); 
imageline($captcha,0,0,39,29,$line);
imageline($captcha,40,0,64,29,$line);
imagestring($captcha, 5, 20, 10, $string, $black); 
$_SESSION["CAPTCHA_CHALLENGE"] = md5($string);
header("Content-type: image/png");
imagepng($captcha);

$result = " ";
if($_GET["action"] == "proc")
{
$response = $_POST["response"];

if($response != $_SESSION["CAPTCHA_CHALLENGE"])
	$result = "The code you entered was incorrect.";
else
	$result = "Correct!";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
        <title>Test</title>
    </head>
<body>
	<div><?php echo $result; ?></div>
	<div id="test">
		<h1>This is a test<h1>
		<form name="x" method="POST" action="Test.php?action=proc">
			Type the code on the right:
			<input type="text" name="response" size="7" maxlength="7"/>
			<img src="./img/captcha.PNG"/>
		</form>
	</div>
</body>
</html>

 

Now what is happening is that the page is loading with the captcha at the top-left, but all the HTML/form content is being ignored, probably because I'm telling the page to display image/png content.

 

Any ideas?

 

Thanks always,

ZHarvey

Link to comment
https://forums.phpfreaks.com/topic/201780-php-captcha-issue/#findComment-1058431
Share on other sites

Move everything from $md5 = ... to imagepng($captcha); to a new file called image_gen.php, or w/e you want to call it.  Make sure you start a session on that page as well.

 

In your original document, make sure that the session is still started.

 

Wherever you want the captcha, put this:

 

<img src='image_gen.php' alt='Captcha Image' />

 

<?php
//image_gen.php
session_start();
// Make captcha
$md5 = md5(microtime() * mktime());
$string = substr($md5,0,7);
$captcha = imagecreatefrompng("./img/captcha.PNG");
$black = imagecolorallocate($captcha, 0, 0, 0);
$line = imagecolorallocate($captcha,233,239,239);
imageline($captcha,0,0,39,29,$line);
imageline($captcha,40,0,64,29,$line);
imagestring($captcha, 5, 20, 10, $string, $black);
$_SESSION["CAPTCHA_CHALLENGE"] = md5($string);
header("Content-type: image/png");
imagepng($captcha);
?>

 

<?php
// Original file
session_start();
$result = " ";
if($_GET["action"] == "proc"){	
$response = $_POST["response"];		
if($response != $_SESSION["CAPTCHA_CHALLENGE"])
$result = "The code you entered was incorrect.";
else		$result = "Correct!";
}?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
        <title>Test</title>
    </head>
<body>
	<div><?php echo $result; ?></div>
	<div id="test">
		<h1>This is a test<h1>
		<form name="x" method="POST" action="Test.php?action=proc">Type the code on the right:	
<input type="text" name="response" size="7" maxlength="7"/>
<img src="image_gen.php" alt='captcha image' /></form>	</div>	</body></html>

 

I apologize about the white space issue, my copy and paste is not working right.

 

 

Link to comment
https://forums.phpfreaks.com/topic/201780-php-captcha-issue/#findComment-1058437
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.