-1

Possible Duplicate:
Does html can be use with dynamic generated images in php?

I'm trying to generate captcha in php. I believe i've the code right, but i'm not able to get the image on the browser..this is the code:

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php header('Content-type: image/png');?>
<?php session_start();
$md5 = md5(microtime() * time() );
$string = substr($md5, -5);
$captcha = imagecreatefrompng("./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);
$_SESSION['key'] = md5($string);
imagestring($captcha, 5, 20, 10, $string, $black);
imagepng($captcha);?> 
</body>
</html>

the png image is on the same folder as this code. the GD option is enabled in php..I a clueless..any help is appreciated...thank you

Community
  • 1
  • 1

2 Answers2

0

You cant output anything before setting the <?php header('Content-type: image/png');?> or the session_start();

You need to create an image script that handles the image and then link to that script within your html

example: captcha.php

<?php 
session_start();
header('Content-type: image/png');
$md5 = md5(microtime() * time() );
$string = substr($md5, -5);
$captcha = imagecreatefrompng("./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);
$_SESSION['key'] = md5($string);
imagestring($captcha, 5, 20, 10, $string, $black);
imagepng($captcha);
?>

Your HTML

<?php session_start(); ?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<img src="captcha.php"/>
</body>
</html>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

I guess this is the source of your CAPTCHA image (e.g. a "image.php" file), and it is loaded from somewhere else (e.g. from "captcha.php" with <img src="image.php" />). You may have to include session_start() in the "captcha.php" file too.

From the code you posted, just remove all HTML.

Also, as a rule, never send an Image content-type until you're ready (and check for errors first, if suitable).

<?php
    session_start();
    $md5 = md5(microtime() * time() );
    $string = substr($md5, -5);
    $captcha = imagecreatefrompng("./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);
    $_SESSION['key'] = md5($string);
    imagestring($captcha, 5, 20, 10, $string, $black);
    Header('Content-type: image/png');
    imagepng($captcha);
?>

NOTE: you are running the MD5 of a MD5 string. Is that correct? Why not use uniqid() instead of md5(microtime() * time() ), and store $md5 in the_SESSION?

LSerni
  • 55,617
  • 10
  • 65
  • 107