...

View Full Version : problems setting captcha code



catania
09-11-2011, 03:22 AM
Hi,
I'm trying to implement a captcha code. This is working fine:

<?php
function imagen() {
header("Content-type: image/png");
$img = imagecreatetruecolor(240,60);

$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$grey = imagecolorallocate($img, 150, 150, 150);
$pink = imagecolorallocate($img, 200, 0, 150);
$font = "./erasbd.ttf";

imagefill($img, 0, 0, $grey);

$string = random_string(rand(7,8));

imagettftext($img, 26, 5, 13, 45, $black, $font, $string);
imagettftext($img, 26, 5, 15, 47, $pink, $font, $string);
imagettftext($img, 26, 3, 18, 49, $white, $font, $string);

$imagen = imagepng($img);
$fin_magine = imagedestroy($img);


if(isset($_FILES[$imagen][$fin_magine])) {
echo $imagen; $fin_magine;
} else {
}
}
imagen();

?>

but when trying to use it inside html it will return: The image “http://........./captcha3.php” cannot be displayed because it contains errors.

here's how I placed it


table
tr
td <?php
function imagen() {
header("Content-type: image/png");
$img = imagecreatetruecolor(240,60);

$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$grey = imagecolorallocate($img, 150, 150, 150);
$pink = imagecolorallocate($img, 200, 0, 150);
$font = "./erasbd";

imagefill($img, 0, 0, $grey);

$string = random_string(rand(7,8));

imagettftext($img, 26, 5, 13, 45, $black, $font, $string);
imagettftext($img, 26, 5, 15, 47, $pink, $font, $string);
imagettftext($img, 26, 3, 18, 49, $white, $font, $string);

$imagen = imagepng($img);
$fin_magine = imagedestroy($img);


if(isset($_FILES[$imagen][$fin_magine])) {
echo $imagen; $fin_magine;
} else {
}
}
imagen();
?>
!-- <img src="captcha2.php" / --
/td

/td
td
form action"captcha.php" method"post"

input name="cambiar" type="submit" value="Cambiar"/
/td
tr
tdEscriba el codigo de la imagen que ve arriba/td
/tr
tr
td <input id="captcha" name="captcha" type="text"/
/td
/tr


as you can see i commented out <img src="captcha2.php" which I innitially tried but y idea is to place it as a function so I can validate that with the imput value. Please let me know if there's any solution.

Most appreciated!
Carlos

mlseim
09-11-2011, 08:43 PM
Once you've set the content-type for images, you can no
longer put anything else in the output. Keep the image processing
all to itself, and reference it only from <img src> tags in other scripts.

If doing this gets too hard ... try using recaptcha (it's free and easy to use).

http://www.google.com/recaptcha


.

gerald
09-11-2011, 10:08 PM
I'd stick the PHP code to create the Captcha image into a seperate file, e.g. captcha.php, then call it using <img src="captcha.php">. If you need to pass values through to it, why not reference them as: <img src="captcha.php?value1=value&value2=value"> ?

Gerald.

BluePanther
09-11-2011, 10:21 PM
If you're doing this for a commercial reason, I would suggest just using recapcha. It's a pretty decent, free library that requires minimal effort.

But, if it's just a project for your own learning, then it's not a bad one to do. Defining a header content-type makes apache parse the output as that content-type. Meaning, the output of the .php file in your case, is the same as the browser looking at a .jpg. This means that echo's etc. are unsuitable for your file, as your file IS an image being created by the php, not a file that is displaying an image. Like gerald said, if this file was called image.php, you would display the image by using the filepath+image.php in an img tag in html.
Also, like gerald said, you can insert parameters using the GET URI statements you would normally use, but for a capcha script I doubt they'd be needed.

As for actually validating the captcha, you could use session variables to set the answer required (setting the session variables in the actual image.php), then on the form processing, compare the form input with the session variables you set.

tangoforce
09-12-2011, 01:48 AM
Hi,
“http://........./captcha3.php” cannot be displayed because it contains errors.


Yes of course it will.

In your html you always use the <img> tag for displaying images don't you. Captcha images are no different. They must still be placed into the html by an <img> tag.

If you think about it, when was the last time you looked at html source and saw an actual image in the source code? ;) Never. You'll see a URL to it instead of an image.

The reason your browser is saying the image contains errors is because outputting it in the middle of the html corrupts the file stream for the image. The image is no longer just jpeg data its also got bits of html inside it which corrupts the file information contained within and thus your browser detects the corruption and doesn't display the image. The same thing happens if you have a seperate script for captcha images but have whitespace before or after the <? and ?> tags.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum