that old header problem...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fael097
Forum Commoner
Posts: 34
Joined: Sat Mar 06, 2010 7:57 pm

that old header problem...

Post 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>";
?>
mikeman
Forum Newbie
Posts: 18
Joined: Wed Apr 07, 2010 7:18 am

Re: that old header problem...

Post 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();
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: that old header problem...

Post by Reviresco »

Set the cookie before you echo the HTML tags.
Post Reply