Jump to content

headers allready sent - newbie problem


Aero77

Recommended Posts

I got this code

<?php
// Check if session is not registered, redirect back to main page. 
// Put this code in first line of web page. 
session_start();
if (!isset($_COOKIE["user"])) {
header("location:login.php");
}
include 'connection.php';
?>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="HandheldFriendly" content="true">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> 
<title>EBS Service Skjema</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<?php
include 'menu.php';
echo "<div class=\"bluebox\">";

if (isset($_GET['sted'])) {

 require_once('graph/jpgraph.php');
 require_once('graph/jpgraph_line.php'); 
 include 'connection.php';
 //  data

	$sql = mysqli_query($con, "SELECT * FROM diesel WHERE sted = '".$_GET['sted']."' GROUP BY WEEK(dato)");

	    
$ydata = array();	
$ydate = array(); 
while($row = mysqli_fetch_array($sql))
		{
			$ydata[]=$row['krl'];
			$ydate[]=$row['dato'];
		}


// Create the graph. These two calls are always required
$graph = new Graph(600,250);
$graph->SetScale('textlin');
$graph->title->Set('Pris Historikk');
$graph->xaxis->title->Set("Dato");
$graph->yaxis->title->Set("Pris");
$graph->xaxis->SetTickLabels($ydate);
// Create the linear plot
$lineplot=new LinePlot($ydata);
$lineplot->SetColor('blue');
//$lineplot->SetFillColor('orange@0.5');

// Add the plot to the graph
$graph->Add($lineplot);

// Display the graph
$graph->Stroke();
}

else {
	echo "Du får ikke tilgang på denne måten.";
}


echo "</div>";
?>

</body>
</html>

but I get headers allready sent. I was earlier told to learn and code or make my apps different, but I'm totaly stuck cause I need to make a page with those stats. Not sure how I can make all my code be processed first then put into the html area. Is a function the way to go here ?

 

Thanks for reading and hope someone can enlighten me :D

Link to comment
Share on other sites

It says 21 and thats where the starting <?php tag is. I'm using jpGraph and the error output is slightly different. Here it is

 

 

 

JpGraph Error: HTTP headers have already been sent.
Caused by output from file history.php at line 21. Explanation:
HTTP headers have already been sent back to the browser indicating the data as text before the library got a chance to send it's image HTTP header to this browser. This makes it impossible for the library to send back image data to the browser (since that would be interpretated as text by the browser and show up as junk text).

Most likely you have some text in your script before the call to Graph::Stroke(). If this texts gets sent back to the browser the browser will assume that all data is plain text. Look for any text, even spaces and newlines, that might have been sent back to the browser.

For example it is a common mistake to leave a blank line before the opening "<?php".

 

Here is line #21

<?php
// Check if session is not registered, redirect back to main page. 
// Put this code in first line of web page. 
session_start();
if (!isset($_COOKIE["user"])) {
header("location:login.php");
}
include 'connection.php';
?>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="HandheldFriendly" content="true">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> 
<title>EBS Service Skjema</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<?php
include 'menu.php';
echo "<div class=\"bluebox\">";

if (isset($_GET['sted'])) {

 require_once('graph/jpgraph.php');
 require_once('graph/jpgraph_line.php'); 
 include 'connection.php';
 //  data

	$sql = mysqli_query($con, "SELECT * FROM diesel WHERE sted = '".$_GET['sted']."' GROUP BY WEEK(dato)");

	    
$ydata = array();	
$ydate = array(); 
while($row = mysqli_fetch_array($sql))
		{
			$ydata[]=$row['krl'];
			$ydate[]=$row['dato'];
		}


// Create the graph. These two calls are always required
$graph = new Graph(600,250);
$graph->SetScale('textlin');
$graph->title->Set('Pris Historikk');
$graph->xaxis->title->Set("Dato");
$graph->yaxis->title->Set("Pris");
$graph->xaxis->SetTickLabels($ydate);
// Create the linear plot
$lineplot=new LinePlot($ydata);
$lineplot->SetColor('blue');
//$lineplot->SetFillColor('orange@0.5');

// Add the plot to the graph
$graph->Add($lineplot);

// Display the graph
$graph->Stroke();
}

else {
	echo "Du får ikke tilgang på denne måten.";
}


echo "</div>";
?>

</body>
</html>
Edited by Aero77
Link to comment
Share on other sites

As the message says you can't output anything before your graph package does its output.  You are sending the html for your page prior to the graph package calls, so that is your problem.  However I'm not entirely sure since line 21 is NOT doing anything so it is a bit confusing.

 

When you said "here is line 21" are we seeing the VERY beginning of the currently running script or a part of the whole thing?

Link to comment
Share on other sites

Well, this is one of those famous PHP issues.

 

Always put the application logic to the very top of the script and the HTML to the very bottom. Do not mix logic with markup like you currently do. Once there's output, PHP can no longer send HTTP headers.

 

So your script should look like this:

<?php

// all the application logic in a single PHP block

?>
<!-- Now the HTML, possibly mixed with short PHP blocks -->
Link to comment
Share on other sites

jpgraph dynamically produces an image (i.e. the selected content type header followed by the binary image data.) you cannot output an image directly on a html web page. you must use an <img src='url_that_results_in_an_image'> tag on your web page to cause an image to be rendered by the browser, where the url_that_results_in_an_image would be a url to a .php script that uses jpgraph inside that .php script.

 

most of the jpgraph examples included with the jpgraph download are the .php code that would go in the file that's supplied as the url to the <img src = ' ... '> tag.

 

you can browse directly to the .php files with the jpgraph code in them, because the browser can figure out what to do based on the content type header/binary image data the the file sends, but in order to put a dynamically created image onto a web page, you must use an <img src = ' ... '> tag.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.