sybil6
06-16-2010, 03:21 PM
hi
i need to include a captcha in a form, the code is a simple captcha wich goes on like this:
<?php
session_start();
//Now lets use md5 to generate a totally random string
$md5 = md5(microtime() * mktime());
/*
We dont need a 32 character long string so we trim it down to 5
*/
$string = substr($md5,0,5);
/*
Now for the GD stuff, for ease of use lets create
the image from a background image.
*/
$captcha = imagecreatefrompng("./CAPTCHA.png");
/*
Lets set the colours, the colour $line is used to generate lines.
Using a blue misty colours. The colour codes are in RGB
*/
$black = imagecolorallocate($captcha,221, 36, 175);
$line = imagecolorallocate($captcha,233,239,239);;
/*
Now to make it a little bit harder for any bots to break,
assuming they can break it so far. Lets add some lines
in (static lines) to attempt to make the bots life a little harder
*/
imageline($captcha,0,0,39,29,$line);
imageline($captcha,40,0,64,29,$line);
/*
Now for the all important writing of the randomly generated string to the image.
*/
imagestring($captcha, 5, 20, 10, $string, $black);
/*
Encrypt and store the key inside of a session
*/
$_SESSION['key'] = md5($string);
/*
Output the image
*/
header("Content-type: image/png");
imagepng($captcha);
?>
it works when i test it on a single page but when i include it on my web page i have a problem with the headers already sent stuff:
<label for="saisir_code">
Veuillez saisir le code <SUP class="oblig">*</SUP><br>
<input type="text" name="saisir_code" id="saisir_code" class="text">
</label>
</td>
<td style="padding-left: 100px">
<?php
$md5 = md5(microtime() * mktime());
$string = substr($md5,0,5);
$captcha = imagecreatefrompng("includes/images/CAPTCHA.png");
$black = imagecolorallocate($captcha,221, 36, 175);
$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);
header("Content-type: image/png");
imagepng($captcha);
?>
</td>
<td>
</td>
</tr>
<tr>
so i get this alreday sent headers error , i cannot see how to place the imagepng($captcha); code because if i put it on top of page the whole page will be a png.
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\my\www3\includes\_top.php:23) in C:\Program Files\xampp\htdocs\my\www3\includes\_myin.php on line 222
‰PNG IHDR‹ _cAPLTE$RˆIDAT8A ^xd˜hœKq=”؍SgEc4Re[ZRn/#4‡—€MJ”-‹„‘00P62–‘9_krS$L@&‡]A#“"“wO;dAMLY}Nš™š. CA‡[OŒ&Ntu:y@Xl4'zv[jW/f2b[fN\}1sJŒvvdUo/mŒ$6IENDB`‚
i need to include a captcha in a form, the code is a simple captcha wich goes on like this:
<?php
session_start();
//Now lets use md5 to generate a totally random string
$md5 = md5(microtime() * mktime());
/*
We dont need a 32 character long string so we trim it down to 5
*/
$string = substr($md5,0,5);
/*
Now for the GD stuff, for ease of use lets create
the image from a background image.
*/
$captcha = imagecreatefrompng("./CAPTCHA.png");
/*
Lets set the colours, the colour $line is used to generate lines.
Using a blue misty colours. The colour codes are in RGB
*/
$black = imagecolorallocate($captcha,221, 36, 175);
$line = imagecolorallocate($captcha,233,239,239);;
/*
Now to make it a little bit harder for any bots to break,
assuming they can break it so far. Lets add some lines
in (static lines) to attempt to make the bots life a little harder
*/
imageline($captcha,0,0,39,29,$line);
imageline($captcha,40,0,64,29,$line);
/*
Now for the all important writing of the randomly generated string to the image.
*/
imagestring($captcha, 5, 20, 10, $string, $black);
/*
Encrypt and store the key inside of a session
*/
$_SESSION['key'] = md5($string);
/*
Output the image
*/
header("Content-type: image/png");
imagepng($captcha);
?>
it works when i test it on a single page but when i include it on my web page i have a problem with the headers already sent stuff:
<label for="saisir_code">
Veuillez saisir le code <SUP class="oblig">*</SUP><br>
<input type="text" name="saisir_code" id="saisir_code" class="text">
</label>
</td>
<td style="padding-left: 100px">
<?php
$md5 = md5(microtime() * mktime());
$string = substr($md5,0,5);
$captcha = imagecreatefrompng("includes/images/CAPTCHA.png");
$black = imagecolorallocate($captcha,221, 36, 175);
$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);
header("Content-type: image/png");
imagepng($captcha);
?>
</td>
<td>
</td>
</tr>
<tr>
so i get this alreday sent headers error , i cannot see how to place the imagepng($captcha); code because if i put it on top of page the whole page will be a png.
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\my\www3\includes\_top.php:23) in C:\Program Files\xampp\htdocs\my\www3\includes\_myin.php on line 222
‰PNG IHDR‹ _cAPLTE$RˆIDAT8A ^xd˜hœKq=”؍SgEc4Re[ZRn/#4‡—€MJ”-‹„‘00P62–‘9_krS$L@&‡]A#“"“wO;dAMLY}Nš™š. CA‡[OŒ&Ntu:y@Xl4'zv[jW/f2b[fN\}1sJŒvvdUo/mŒ$6IENDB`‚