Function-session-start (Registratin form)

Hi guys!

I am working on registration form, where user need to put CAPTCHA image base.
(where user can put some random text string five character long)

1 -However during the registration form the text image doesn’t not appear.

2 –i wrote captcha file , (for text image create)

3 – found some error on captcha file. (With the help of error.php)

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/mardan/public_html/user-registration/lib/error.php:2) in /home/mardan/public_html/user-registration/img/captcha.php on line 19

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/mardan/public_html/user-registration/lib/error.php:2) in /home/mardan/public_html/user-registration/img/captcha.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /home/mardan/public_html/user-registration/lib/error.php:2) in /home/mardan/public_html/user-registration/img/captcha.php on line 20

Warning: Cannot modify header information - headers already sent by (output started at /home/mardan/public_html/user-registration/lib/error.php:2) in /home/mardan/public_html/user-registration/img/captcha.php on line 42
‰PNG IHDRA èzÂB PLTE3fÿÿÿÿï@nÂGIDAT ™c ÈÏH>øà ˆÅ&“–l8 Ì’Ë1“怳ÀêØ¤sŒ¬ ,ÃÛ u’o7Hó€Y o>ÁXÏ ¦M¾ 1™TѾ •u $¢IEND®B

4 - This is my function file which i use as include’…/lib/function.php’;

<?php
//return a string of random text of a desired length
function random_text($count, $rm_similar = false )
{
//create list of characters
$chars = array_flip(array_merge(range(0,9), range(‘A’,‘Z’)));
//remove similar looking characters that might cause confusion
if ($rm_similar)
{
unset ($chars[0], $chars[1],$chars[2],$chars[5],$chars[8],$chars[‘B’],$chars[‘I’],$chars[‘o’],$chars[‘Q’],$chars[‘S’],$chars[‘U’],$chars[‘V’],$chars[‘Z’]);
}
//generate the string of random text
for($i = 0, $text = ‘’ ; $i< $count; $i++)
{
$text.= array_rand($chars);
}
return $text;
}
?>


Any idea ,what going wrong.

Please
Thanks

It’s saying that error.php is outputting something before you call session_start(). Where are you including this error.php and why is it outputting something?

for complete understanding of the problem read here: http://www.satya-weblog.com/2007/05/warning-cannot-modify-header.html


can you please give more detalis . please

There are 8 lines missing in the code you posted (the line counter starts with 9). Please post them (and there’s no need to post them as an image, you can copy and paste the code as text, just remember using the correct tags around it).

The error you’re getting means that something has been outputted before the script arrives at the session_start. That’s why it’s good practice putting the session_start at the beginning of the script. In your case, before the includes, and who knows, maybe even before the 8 missing lines.

The ‘output’ can be an echo statement, a line of HTML, but also a simple white line in your script or in one of your includes. A white line means an empty line outside of the <?php and ?> tags.


all of my file is here

<!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=iso-8859-1” />
<title></title>
</head>

<body>
<?php
include ‘…/lib/function.php’;

// must start or continue session and save CAPTCHA string in $_SESSION for it to be
//available to other requests
if(!isset($_SESSION))
{
session_start();
header(‘Cache-control: private’);
}

// create a 65* 20 pixel image
$width =65;
$height=20;
$image = imagecreate(65,20);
//fill the image background color
$bg_color = imagecolorallocate($image,0x33,0x66,0xFF);
imagefilledrectangle($image,0,0,$width,$height,$bg_color);
// fetch random text
$text = random_text(5);
//determine x and y coordinates for centering text
$font=5;
$x = imagesx ($image) / 2 - strlen($text) * imagefontwidth($font) /2;
$y = imagesy ($image) / 2 - imagefontheight($font) / 2 ;
// write text on image
$fg_color = imagecolorallocate($image,0xFF,0xFF,0xFF);
imagestring($image,$font,$x,$y,$text,$fg_color);
// save the CAPTCHA string for latr comparison
$_SESSION[‘captcha’] = $text;

// out put the image
header(‘Content-type: image/png’);
imagepng($image);

imagedestroy($image);
?>
</body>
</html>

(Book Sources this script from : PHP and MSYQL Create-Modify-Reuse
By Tim Boronczyk with Martin E.Psinas , whiley publishing,)

This script’s output is supposed to be an image, delete all that HTML. HTML doesn’t belong in the binary data of an image.

And it’s also the reason for the error. In this case you don’t need it, so like Dan says, delete it. But if you did need it, then you’d have to put the part of the code with the session_start before the HTML code.

Thanks ,Prakash, Dan, and specially guido , its really help full and my error is sort out,

thanks for your help