Warning: Cannot modify header information - headers already

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
soma56
Forum Newbie
Posts: 11
Joined: Fri Jul 10, 2009 12:12 pm

Warning: Cannot modify header information - headers already

Post by soma56 »

I'm sure there's a very simple solution to this that I can't see. I'm bringing in some form data and if one field is empty then I'd like the user redirected to a different page.

Code: Select all

//Info brought in from form
$engravedtext = trim($_POST["engravedtext"]);  

if ($engravedtext=="")
{ 
   //go here
   header ("page1.php");
} 
else 
{ 
   //else go there
   header ("page2.php");
} 


?>
I'm brand new to php and everything 'logically looks right'. If the engraved text field is empty go to this page, if not go to that page. I tried using the 'redirect' function as well with no luck. Suggestions?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Warning: Cannot modify header information - headers alre

Post by Christopher »

You can't have any output before the header(). Check for any echo or characters outside php tags.
(#10850)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Warning: Cannot modify header information - headers alre

Post by Eran »

Also in the future, please use the forum search function before posting. This is just from a couple of days ago
viewtopic.php?f=1&t=115746
soma56
Forum Newbie
Posts: 11
Joined: Fri Jul 10, 2009 12:12 pm

Re: Warning: Cannot modify header information - headers alre

Post by soma56 »

Christopher wrote:You can't have any output before the header(). Check for any echo or characters outside php tags.
Not too sure I understand, but it led me to an hour of reading in which I was able to solve my issue. Thanks Christopher.

All I wanted to do was to redirect a user to one of two pages based on whether or not a particular field was empty (or not). In this case it was 'text engraving'.

Code: Select all

<?PHP

if ($engravedtext=="")   
{    
   echo '<META HTTP-EQUIV="Content="0; URL=success.php">';      
}    
else    
{    
   echo '<META HTTP-EQUIV="Content="0; URL=retry.php">';      
}  

?>
pytrin wrote:Also in the future, please use the forum search function before posting. This is just from a couple of days ago
viewtopic.php?f=1&t=115746
Being completely new to php I'm not certain how this page can help me at this time, perhaps in a few months I'll understand it better :)
Post Reply