HeadersAlready Sent

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
jm00730
Forum Newbie
Posts: 13
Joined: Tue Feb 01, 2011 1:32 pm

HeadersAlready Sent

Post by jm00730 »

I have been killing myself trying to find out how to get around this issu. I am using SVG GRaph to dynamically plot a graph based on the user and user selection. The graph works on it's own page, however i need to include it within another page which has the page header as an include among others. when I do so it does not work because the header include has already sent the header information.

How can I work around this? I have looked into ob_start()/ ob_flush_end() but am not sure how this works when both the header and the page included both send header information. Any ideas?

Below is the code, Any help with this would be very much appreciated as I have been dealing with this for quite some time now

The main page

Code: Select all

<?php

include("include/header.inc");  //If I remove this it works, but does not display the header banner and nav menu
include("include/dbc.php");
require('graphs/SVGraph.php');

global $id;

//get desired view from user selection on the body page
$part = $_POST['BodyPart'];
$view = $_POST['view'];

//===============================================================================================
$graph = new LineGraph();

	$graph->setGraphTitle('My Body Graph');
	$graph->setGraphDescription('Measurement over time');
	$graph->setXAxisLabel('Time');
	$graph->setYAxisLabel('Measurements');



switch ($view){


case "Month":	 // -- month VIEW --- =============================================================

			 $partQuery = mysql_query("SELECT $part FROM indBody WHERE MemberID = '$id' AND  DateEntered >= DATE_SUB(CURDATE(), INTERVAL 1 Month)  ORDER BY DateEntered");


			 $count =  mysql_query("SELECT COUNT( DISTINCT DateEntered, $part FROM indBody) WHERE MemberID = '$id' AND  DateEntered >= DATE_SUB(CURDATE(), INTERVAL '$view' DAY) AND CURDATE()    ORDER BY DateEntered");

			 $plots = array();
			 $dates = array();

			 //create part variables
			  $i = 1;

				   while($fetchPart = mysql_fetch_array($partQuery)) {

						  $plot = $fetchPart[$part];
						  $plots[] = $plot;
						  $date = $fetchPart['DateEntered'];
						  $dates[] = sprintf("%02s/%02s", substr($date,0,4),substr($date,5,6));

					  $i++;

				   }//end while loop

			   //Define some data
				$graph->setXAxis($dates);
			    $graph->addDataRow($plots, 'my data row');

		  //output graph
		  $graph->output();
	  break;


default:	// -- Any other selection VIEW --- =============================================================

			 $partQuery = mysql_query("SELECT DISTINCT DateEntered, $part FROM indBody WHERE MemberID = '$id' AND  DateEntered >= DATE_SUB(CURDATE(), INTERVAL '$view' DAY) AND CURDATE()    ORDER BY DateEntered");

			 $count =  mysql_query("SELECT COUNT(DISTINCT DateEntered, $part FROM indBody) WHERE MemberID = '$id' AND  DateEntered >= DATE_SUB(CURDATE(), INTERVAL '$view' DAY) AND CURDATE()    ORDER BY DateEntered");

			 $plots = array();
			 $dates = array();

			 //create part variables
			  $i = 1;

				   while($fetchPart = mysql_fetch_array($partQuery)) {

						  $plot = $fetchPart[$part];
						  $plots[] = $plot;
						  $date = $fetchPart['DateEntered'];
						  $dates[] = sprintf("%02s/%02s", substr($date,0,4),substr($date,5,6));

					  $i++;

				   }//end while loop

			   //Define some data
				$graph->setXAxis($dates);
			    $graph->addDataRow($plots, 'my data row');

		  //output graph
		  $graph->output();

  }//end switch statement

Here is the header

Code: Select all

<?php session_start();?>
<!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>Food Fitness First</title>

<link rel="stylesheet" type="text/css" href="./css/master.css" />
<link rel="stylesheet" type="text/css" href="./css/pro_drop.css" />
 <link href="calendar/style/style.css" rel="stylesheet" type="text/css" />

<script src="jquery.js" type="text/javascript"></script>
<script src="scripts/stuHover.js" type="text/javascript"></script>

<script type="text/javascript">
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
	try {
	var pageTracker = _gat._getTracker("UA-12068429-1");
	pageTracker._trackPageview();
	} catch(err) {}
</script>

<meta name="google-site-verification" content="o7UaHcuDso80W1_P1K3-Q_IRjY6vmBzN3lQAaiVSnqI" />

</head>

<body>

<!-- Header div -->
<div id="header">
  <div id="search">
	<div id="cse-search-form" style="width: 100%;">Loading</div>
	<script src="http://www.google.com/jsapi" type="text/javascript"></script>
	<script type="text/javascript">
	  google.load('search', '1', {language : 'en'});
	  google.setOnLoadCallback(function(){
		var customSearchControl = new google.search.CustomSearchControl('009126769682394590088:l17wlvfwrvq');
		customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
		var options = new google.search.DrawOptions();
		options.setSearchFormRoot('cse-search-form');
		customSearchControl.draw('cse', options);
	  }, true);
	</script>
	
  </div>
  
  <a href="http://foodfitnessfirst.com"><img src="./images/header2.png" alt="Food Fitness First, Inc." width="800" border="0" /></a>
 
    <!-- Section area - Individual, Corporate, Professionals - div -->
    <center>
        <span class="preload1"></span>
        <span class="preload2"></span> 
 
 	<!-- Individual Area options -->		
        <ul id="nav">
            <li class="top"><a href="index.php" class="top_link"><span class="down">&nbsp;Overview</span></a>
				<ul class="sub">
                	<li><a href="calendar.php">Calendar</a></li>
					<li><a href="reports.php">Reports</a></li>
                </ul>
			</li>
            <li class="top"><a href="profile.php" class="top_link"><span class="down">&nbsp;Myself</span></a>
				<ul class="sub">
                	<li><a href="WeightGoal.php">Weight Goal</a></li>
                </ul>
			</li>
            <li class="top"><a href="foods.php" class="top_link"><span>&nbsp;Food Diary</span></a></li>
            <li class="top"><a href="act.php" class="top_link"><span>&nbsp;Exercise Log</span></a></li>
            <li class="top"><a href="body.php" class="top_link"><span>&nbsp;Body</span></a></li>
            <!--<li class="top"><a href="labs.php" class="top_link"><span class="down">Labs</span></a>
				<ul class="sub">
					<li><a href="Medications.php">Medications</span></a></li>
				</ul>
			</li> -->
			
            <li class="top"><a href="weight.php" class="top_link"><span>&nbsp;Weight</span></a></li>
            <li class="top"><a href="#" class="top_link"><span class="down">Community</span></a>
				<ul class="sub">
                	<li><a href="http://foodfitnessfirst.com/forum/">Forums</a></li>
                	<li><a href="contact.php">Send Message to RD</a></li>
                	<li><a href="read.php">Read Messages</a></li>
                </ul>
			</li>
            <li class="top"><a href="http://foodfitnessfirst.com/subscription/login.php" class="top_link"><span class="down">&nbsp;&nbsp;Log In&nbsp;</span></a>
            	<ul class="sub">
                	<li><a href="http://foodfitnessfirst.com/sub.php">Subscriptions</a></li>
                	<li><a href="http://foodfitnessfirst.com/subscription/member.php">My Account</a></li>					
                </ul>
            </li> 																		
        </ul>		
  </center>
  <br />
<?
 $id= $_SESSION['_amember_user']['member_id'];
	$fname= $_SESSION['_amember_user']['name_f'];
	$lname= $_SESSION['_amember_user']['name_l'];
	$user = $fname. " " .$lname; 
	
	if($id > 0 ){
		echo "Welcome ". $user. ",  Not You?, <a href='http://foodfitnessfirst.com/subscription/member.php'> Click here</a>";
	}else{
	echo 'Not logged in!<a href="http://foodfitnessfirst.com/subscription/member.php"> Log in here</a>';	
	}
	?>
    
    
    
</div>

<div id="wrapper">   
  <!-- Main content -->
  <div id="content" >
  	<div id="cse" style="width:100%;"></div>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: HeadersAlready Sent

Post by AbraCadaver »

I'm not an expert in SVG but I can tell you the problems. Your header is output which sends HTTP headers stating that it is a text/html document. Then later you are outputing an SVG which sends its own headers. This won't work I think. What I know won't work is sending the SVG headers after you have called a session start. A rough example of what you want to do is probably an image example: viewtopic.php?f=1&t=110171&p=582727#p582727

Except you would use an embed tag in your HTML that would have its src set to a PHP file that outputs the SVG.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
jm00730
Forum Newbie
Posts: 13
Joined: Tue Feb 01, 2011 1:32 pm

Re: HeadersAlready Sent

Post by jm00730 »

Thanks for the fast response!
What I know won't work is sending the SVG headers after you have called a session start. A rough example of what you want to do is probably an image example: viewtopic.php?f=1&t=110171&p=582727#p582727

Except you would use an embed tag in your HTML that would have its src set to a PHP file that outputs the SVG.

Code: Select all

// show_image.php
$result = mysql_query('SELECT `Picture`, `Type` FROM `db` WHERE `ID` = ' . (int)$_GET['id']);
$row = mysql_fetch_object($result);
header('Content-type: image/' . $row->Type);
echo $row->Picture;
ok, so I understand the part about not being able to rend the headers once the headers have already been sent. If I understand the rest correctly I will be placing my header code to php so that it does not send the header info? then within the grid page I would assign my result set to the header like the following:

Code: Select all

header('Content-type: image/' . $row->Type);
echo $row->Picture;
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: HeadersAlready Sent

Post by AbraCadaver »

I'm not totally sure, but here is an example of how I think it should work:

Code: Select all

<?php 
include("include/header.inc");

global $id; //dunno why this is here

//get desired view from user selection on the body page
$part = $_POST['BodyPart'];
$view = $_POST['view'];
?>
<!-- maybe use embed -->
<embed src="get_svg.php?id=$id&part=$part&view=$view" . . . . . />
<!-- or use object not sure -->
<object data="get_svg.php?id=$id&part=$part&view=$view" . . . . . />
<!-- or maybe iframe -->
<iframe src="get_svg.php?id=$id&part=$part&view=$view" . . . . . />
<?php
include("include/footer.inc");
?>
get_svg.php

Code: Select all

<?php
include("include/dbc.php");
require('graphs/SVGraph.php');

$id = $_GET['id'];
$part = $_GET['part'];
$view = $_GET['view'];

$graph = new LineGraph();

//query database create svg, blah, blah

$graph->output();
?>
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
jm00730
Forum Newbie
Posts: 13
Joined: Tue Feb 01, 2011 1:32 pm

Re: HeadersAlready Sent

Post by jm00730 »

Sorry its taken some time to reply,

I tried your suggestions, the embed option gave me a missing plugin error. The Object method did not display anything. But the iFrame method cleared the headers already sent error. However now I am recieving a different error:

Code: Select all

Warning: Division by zero in /home/fffinc/public_html/i/graphs/LineGraph.php on line 72

Warning: max() [function.max]: Array must contain at least one element in /home/fffinc/public_html/i/graphs/LineGraph.php on line 98

Warning: min() [function.min]: Array must contain at least one element in /home/fffinc/public_html/i/graphs/LineGraph.php on line 99

Warning: Cannot modify header information - headers already sent by (output started at /home/fffinc/public_html/i/graphs/LineGraph.php:72) in /home/fffinc/public_html/i/graphs/functions.inc.php on line 61
That lineGraoh code calls the function to set the xAxis the code is below:

Code: Select all

<?php
include("include/dbc.php");
require('graphs/SVGraph.php');

global $id; //this is here to get the $id from the header.php file...doesnt work without it here

$part = $_GET['part'];
$view = $_GET['view'];


$graph = new LineGraph();

	$graph->setGraphTitle('My Body Graph');
	$graph->setGraphDescription('Measurement over time');
	$graph->setXAxisLabel('Time');
	$graph->setYAxisLabel('Measurements');

$partQuery = mysql_query("SELECT DateEntered, $part FROM indBody WHERE MemberID = '$id' AND  DateEntered >= DATE_SUB(CURDATE(), INTERVAL '$view' DAY) AND CURDATE()    ORDER BY DateEntered");
			 
			 
			$count = count(mysql_query("SELECT COUNT(DateEntered)  FROM indBody WHERE MemberID = '$id' AND  DateEntered >= DATE_SUB(CURDATE(), INTERVAL '$view' DAY) AND CURDATE() "));
			
			
			 $plots = array();
			 $dates = array();

			 //create part variables
			  $i = 1;

				   while($fetchPart = mysql_fetch_array($partQuery)) {

						  $plot = $fetchPart[$part];
						  $plots[] = $plot;
						  $date = $fetchPart['DateEntered'];
						  $dates[] = sprintf("%02s/%02s", substr($date,0,4),substr($date,5,6));
				
					  $i++;

				   }//end while loop

			   //Define some data
				$graph->setXAxis($dates);
			    $graph->addDataRow($plots, 'my data row');

		  //output graph
		  $graph->output();
It seems that using the iFrame is making part of my mysql not return an empty result-set even though there should be something being returned. I cannot understand why this would be. Does anyone have any suggestions?
jm00730
Forum Newbie
Posts: 13
Joined: Tue Feb 01, 2011 1:32 pm

Re: HeadersAlready Sent

Post by jm00730 »

Got it working, turns out my dates werent returning, thanks for all your help it was very much appreciated!
Post Reply