turpentyne Posted April 15, 2011 Share Posted April 15, 2011 probably another overly-newbie-ish question, but I built a series of pages for inputting data. Page 1 inputs data to table 1, pulls the item id and passes it to page 2 to input to table 2. This all works fine. But now I want to add a header and footer to include the site design. And when I add my <? include("header.php"); ?>, it stops working. I tried naming the header something else, in case it was just a duplicate name somewhere, but the problem is still there. When i submit, it goes to the next page and generates this error: Warning: Cannot modify header information - headers already sent by (output started at header.php:62) in file.php on line 65 Is there a way to include a header without it creating this issue? Or do I put it lower down in the code? Here's the main idea of the code: if (isset($_POST['submitted'])) { $errors = array(); $descriptor1 = trim($_POST['plant_id']); $descriptor2 = trim($_POST['existence_status']); $descriptor3 = etc. etc. if (empty($errors)) { require ('connection.php'); $query = "INSERT INTO habits (plant_id, existence_status, reproduction_methods, etc.) VALUES ('$descriptor1', '$descriptor2', etc.)"; $result = @mysql_query ($query); $plant_id=mysql_insert_id(); header ("Location: thisfile.php?var1=$plant_id"); and, if it helps any, here's the header file: <div id="ugg" style="background-color:#CDE472; height:80px; padding:10px;"> <h1>Twigzy</h1> </div> <style type="text/css" media="screen"> /**************** menu coding *****************/ #menu { width: 100%; background: #eee; float: left; } #menu ul { list-style: none; margin: 0; padding: 0; width: 12em; float: left; } #menu a, #menu h2 { font: bold 11px/16px arial, helvetica, sans-serif; display: block; border-width: 1px; border-style: solid; border-color: #ccc #888 #555 #bbb; margin: 0; padding: 2px 3px; } #menu h2 { color: #fff; background: #324F17; text-transform: uppercase; } #menu a { color: #000; background: #efefef; text-decoration: none; } #menu a:hover { color: #a00; background: #fff; } #menu li {position: relative;} #menu ul ul { position: absolute; z-index: 500; } #menu ul ul ul { position: absolute; top: 0; left: 100%; } div#menu ul ul, div#menu ul li:hover ul ul, div#menu ul ul li:hover ul ul {display: none;} div#menu ul li:hover ul, div#menu ul ul li:hover ul, div#menu ul ul ul li:hover ul {display: block;} </style> <!--[if IE]> <style type="text/css" media="screen"> #menu ul li {float: left; width: 100%;} </style> <![endif]--> <!--[if lt IE 7]> <style type="text/css" media="screen"> body { behavior: url(csshover.htc); font-size: 100%; } #menu ul li a {height: 1%;} #menu a, #menu h2 { font: bold 0.7em/1.4em arial, helvetica, sans-serif; } </style> <![endif]--> <!-- start menu HTML --> <div id="menu"> <ul> <li><a href="link"">Home</a> </li> </ul> <ul> <li><h2>About</h2> <ul> <li><a href="link0.com">About site</a> </li> </ul> </li> </ul> <ul> <li><h2>Search for a plant</h2> <ul> <li><a href="link1.php">Basic Search</a></li> <li><a href="link2.php">Search by Leaf</a></li> <li><a href="link3.php">Search by Flower</a></ul> </li> </ul> <ul> <li><h2>Login</h2> <ul> <li><a href="link4.php">Login</a></li> <li><a href="asdf.html">asdfasdf</a><!-- fully working sample --> <ul> <li><a href="test.html">asdf</a> </li> </ul> </li> </ul> </li> </ul> </div> Quote Link to comment https://forums.phpfreaks.com/topic/233847-including-a-headerphp-breaks-process/ Share on other sites More sharing options...
analog Posted April 15, 2011 Share Posted April 15, 2011 The error: Warning: Cannot modify header information - headers already sent by (output started at header.php:62) in file.php on line 65 Is due to the fact that header.php is sending output to the browser. Once output has been sent you can no longer set HTTP headers so this: header("Location: thisfile.php?var1=$plant_id"); is failing. One easy fix is to call ob_start at the top of the header file like this: <?php ob_start(); ?> // rest of header here That will stop any output being sent until either the script has finished or you call ob_end_flush. Quote Link to comment https://forums.phpfreaks.com/topic/233847-including-a-headerphp-breaks-process/#findComment-1202156 Share on other sites More sharing options...
lastkarrde Posted April 15, 2011 Share Posted April 15, 2011 The 'headers already sent' error message is basically telling you that your server has already started sending the HTML/css data in header.php to the client, and then your trying to send the header() redirect to the client. Why do you need to display HTML/css on a page that simply redirects? The user wouldn't see anything on that page anyway, would they? Quote Link to comment https://forums.phpfreaks.com/topic/233847-including-a-headerphp-breaks-process/#findComment-1202157 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.