Page 1 of 1

that old header problem...

Posted: Fri Jun 11, 2010 9:22 am
by fael097
sup folks, im having that old header error:
[text]Warning: Cannot modify header information - headers already sent by (output started at C:\set.php:7) in C:\set.php on line 31[/text]
but my code apparently has no problems, whitespaces, etc.
could someone take a look and see whats wrong? its just a code to test setting a cookie.

Code: Select all

<?php
echo "
<html>
<head>
<title>SET YOUR COOKIE!</title>
</head>
<body>";
if (isset($_COOKIE['usern']))
{
	$usern=$_COOKIE['usern'];
	echo "Username ".$usern."already set.";
}
else
{
echo "
Username:&nbsp;
<form method='post'>
<input type='text' name='name'></input><br />
<input type='submit' name='submit' value='Submit'></input>
</form>";
}
if(isset($_POST['submit']))
{
	if(($_POST['name'])!="")
	{
		$name=$_POST['name'];
		echo $name;
		setcookie('usern', $name, time()+3600);
	}
	else 
	{
		echo "<script language='JavaScript'>alert('You must type a name.');</script>";
	}
}
echo "
</body>
</html>";
?>

Re: that old header problem...

Posted: Fri Jun 11, 2010 10:59 am
by mikeman
I am by no means experienced but you could try this:
Enable output buffering on the script at the very top by using this function:

Code: Select all

ob_start();
and then at the very end of the script flush it with:

Code: Select all

ob_end_flush();

Re: that old header problem...

Posted: Fri Jun 11, 2010 12:09 pm
by Reviresco
Set the cookie before you echo the HTML tags.