Php 无法修改标题信息

Php 无法修改标题信息,php,image,http,post,header,Php,Image,Http,Post,Header,我有一个登录页面,用户在其中选择一个选项。然后它必须显示一个图表。 但是我得到了这个错误 无法修改标题信息-第126行的/var/www/databin/php/visual/chart.php:136中的(输出开始于/var/www/databin/php/visual/chart.php)已发送标题 我试着从论坛上理解,但我做不到 谢谢你的帮助 这是密码 <?php function subval_sort($a,$subkey) { foreach($a as $k=>$v)

我有一个登录页面,用户在其中选择一个选项。然后它必须显示一个图表。 但是我得到了这个错误

无法修改标题信息-第126行的/var/www/databin/php/visual/chart.php:136中的(输出开始于/var/www/databin/php/visual/chart.php)已发送标题

我试着从论坛上理解,但我做不到

谢谢你的帮助

这是密码

<?php
function subval_sort($a,$subkey) {
 foreach($a as $k=>$v) {
  $b[$k] = strtolower($v[$subkey]);
 }
 asort($b);
 foreach($b as $key=>$val) {
  $c[] = $a[$key];
 }
 return $c;
}

function display_graph($str)
{
echo $str;
$data = array( 
"Jan" => 55, 
"Feb" => 54, 
"Mar" => 53, 
"Apr" => 33, 
"May" => 13, 
"Jun" => 15, 
"Jul" => 23, 
"Aug" => 28, 
"Sep" => 32, 
"Oct" => 45, 
"Nov" => 73, 
"Dec" => 71); 

// create image 
$width = 480; 
$height = 250; 
$image = imagecreate($width, $height); 

// colors 
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); 
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80); 
$black = imagecolorallocate($image, 0x00, 0x00, 0x00); 
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0); 

// layout 
$maxval = max($data); 
$nval = sizeof($data); 

$vmargin = 20; // top (bottom) vertical margin for title (x-labels) 
$hmargin = 38; // left horizontal margin for y-labels 

$base = floor(($width - $hmargin) / $nval); // distance between columns 

$ysize = $height - 2 * $vmargin; // y-size of plot 
$xsize = $nval * $base; // x-size of plot 

// title 
$titlefont = 3; 
$title = "Presidential Approval Ratings 2000 (in %)"; 

$txtsz = imagefontwidth($titlefont) * strlen($title); // pixel-width of title 

$xpos = (int)($hmargin + ($xsize - $txtsz)/2); // center the title 
$xpos = max(1, $xpos); // force positive coordinates 
$ypos = 3; // distance from top 

imagestring($image, $titlefont, $xpos, $ypos, $title , $black); 

// y labels and grid lines 
$labelfont = 2; 
$ngrid = 4; // number of grid lines 

$dydat = $maxval / $ngrid; // data units between grid lines 
$dypix = $ysize / ($ngrid + 1); // pixels between grid lines 

for ($i = 0; $i <= ($ngrid + 1); $i++) { 
    // iterate over y ticks 

    // height of grid line in units of data 
    $ydat = (int)($i * $dydat); 

    // height of grid line in pixels
    $ypos = $vmargin + $ysize - (int)($i*$dypix); 

    $txtsz = imagefontwidth($labelfont) * strlen($ydat); // pixel-width of label 
    $txtht = imagefontheight($labelfont); // pixel-height of label 

    $xpos = (int)(($hmargin - $txtsz) / 2); 
    $xpos = max(1, $xpos); 

    imagestring($image, $labelfont, $xpos, 
        $ypos - (int)($txtht/2), $ydat, $black); 

    if (!($i == 0) && !($i > $ngrid)) {
        imageline($image, $hmargin - 3, 
            $ypos, $hmargin + $xsize, $ypos, $gray); 
        // don't draw at Y=0 and top 
    }
} 

// columns and x labels 
$padding = 3; // half of spacing between columns 
$yscale = $ysize / (($ngrid+1) * $dydat); // pixels per data unit 

for ($i = 0; list($xval, $yval) = each($data); $i++) { 

    // vertical columns 
    $ymax = $vmargin + $ysize; 
    $ymin = $ymax - (int)($yval*$yscale); 
    $xmax = $hmargin + ($i+1)*$base - $padding; 
    $xmin = $hmargin + $i*$base + $padding; 

    imagefilledrectangle($image, $xmin, $ymin, $xmax, $ymax, $navy); 

    // x labels 
    $txtsz = imagefontwidth($labelfont) * strlen($xval); 

    $xpos = $xmin + (int)(($base - $txtsz) / 2); 
    $xpos = max($xmin, $xpos); 
    $ypos = $ymax + 3; // distance from x axis 

    imagestring($image, $labelfont, $xpos, $ypos, $xval, $black); 
} 

// plot frame 
imagerectangle($image, $hmargin, $vmargin, 
    $hmargin + $xsize, $vmargin + $ysize, $black); 

// flush image 
header("Content-type: image/gif"); // or "Content-type: image/png" 
imagegif($image); // or imagepng($image) 
imagedestroy($image); 
}
?>

<html>
<body>
<form method="GET"  action="">
<input type="submit" name="name" value="Submit">
<?php

    echo "<select name=\"name\">";
    echo "<option size =30 selected>Select</option>";

 $initDir = "/var/www/databin/userfiles/";
  $peoplearr = scandir ($initDir);

foreach ($peoplearr as $person)
  {
    if (strcmp ($person, ".") == 0)
      continue;
    if (strcmp ($person, "..") == 0)
      continue;
    $str = str_replace(".html","",$person);

    echo "<option>$str</option>";

  }
echo "</select>";
if(isset($_GET['name']))
{
echo $_GET['name'];
$user = $_GET['name'];
$initdir = "/var/www/databin/userfiles/";
$person = $user.".html/";
$person_comparisons = array();
$display_arr       = array();
$str = file_get_contents($initdir.$person."person_comparison");
$person_comparisons = json_decode($str,true);
$i = 0;
if($person_comparisons[$i])
{
while(true)
 {
 $display_arr[$i]['name'] = $person_comparisons[$i]['name'];
 $display_arr[$i]['sim'] = $person_comparisons[$i]['sim'];
 $i = $i + 1;
 if(!$person_comparisons[$i]) break;
 }

$display_arr = subval_sort($display_arr,'sim'); 
print_r($display_arr);
display_graph($display_arr);
}// end if

}

您在其他输出打印之后打印标题,这是错误的,这就是它抱怨的原因。
要修复它,您必须确保在调用之前没有输出

header()
有时,它也可能依赖于标记之前的空白,因此请确保在第一个标记之前没有空白 如果不是简单的情况,请检查在任何标头调用之前是否没有打印/回显

编辑:

好吧,这肯定不是空格的情况

最好的方法可能是将图像创建移动到不同的php脚本(如graph_image.php)中,然后移动到希望图形执行以下操作的位置

<img src="graph_image.php?name=<?php print $_GET['name'];" />


通过将顶部的所有代码以及创建数组的大部分代码或作为参数发送给随后创建图像的函数的任何内容移动到该函数来完成此操作。

典型错误。display_graph()函数调用:

header("Content-type: image/gif"); // or "Content-type: image/png"
这应该是输出的第一件事

在您的例子中,您在同一页面上同时生成html内容和png图像,这是错误的。将png部分移动到一个单独的文件,即display_graph.php,并从html代码中这样调用它:

<img src="display_graph.php?param1=1&param2=2"/>

@SLaks我懒得清理,谢谢:)