100分! 简单的问题,我一旦运行header( ); 必然告知:Cannot modify header information ........

norsd 2005-05-30 01:30:33
我一旦运行header( ); 必然告知:Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\test.php:1) in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 1

我发誓没有空格或者空行,我的程序这样:
<html><head><title>First program</title></head><body><script language="php">header("Content-type:image/png");
$img_handle = ImageCreate (230, 20) or die ("Cannot Create image");
$back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
$txt_color = ImageColorAllocate ($img_handle, 233, 114, 191);
ImageString ($img_handle, 31, 5, 5, "My first Program with GD", $txt_color);
ImagePNG ($img_handle);
</script>
</body>
</html>

如果我去除 header(....); 这个,程序就不会报错,当然也不会显示图像了。
求救!郁闷死了。谢谢
...全文
223 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
indexroot 2005-05-30
  • 打赏
  • 举报
回复
文件头Content-type:image/png
说明输出是图片格式
就不能再定义为html文件的Title

要有title
就写html文件
程序输出头header("Content-type:text/html");默认不用写
在其中显示图片
<img src="***.php">

***.php
程序输出头header("Content-type:image/png");
定义文件类型为image/png
专门输出图片

文本和图片数据不能混在一起
viewpl 2005-05-30
  • 打赏
  • 举报
回复

Content-type:image/png 的意思是告诉浏览器这个文件是个图片不是html。

所以你不能直接在地址栏输入地址去访问该页,否则就是乱码

你应该新建另一个php页,用 <img src="图片页.php"></img> 去显示这个图片





zhiin1 2005-05-30
  • 打赏
  • 举报
回复
第一步: 把上面的文件命名为"pic.php"
第二步: 新建一个文件名为"index.php"
第三步: 在index.php里这样编写!

<?php
echo "我的图片为:";
echo "<img src='pic.php'>";
?>
norsd 2005-05-30
  • 打赏
  • 举报
回复
我现在发现,我如果显示了图片,我就不能干其他的输出了。 如果我echo "...." 都不会正确显示,请问怎么解决这个问题? 阿,我头大了。
xuzuning 2005-05-30
  • 打赏
  • 举报
回复
去掉前后的html代码
既然你header("Content-type:image/png");
这就表示其后的输出内容将被当作图片的内容。只有合法的信息才会被解释成图片,否则就是乱码
图片是没有title的
zhiin1 2005-05-30
  • 打赏
  • 举报
回复
TO netvt(唯她(为了泡老婆,努力学习LISP)):

晕! 难道下面这样不标准? 我觉得应该根据情况而定!


<?php
header("Content-type:image/png");
$img_handle = ImageCreate (230, 20) or die ("Cannot Create image");
$back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
$txt_color = ImageColorAllocate ($img_handle, 233, 114, 191);
ImageString ($img_handle, 31, 5, 5, "My first Program with GD", $txt_color);
ImagePNG ($img_handle);
?>

这样已经够标准了~~~~~~~ 呵呵
zhiin1 2005-05-30
  • 打赏
  • 举报
回复
还是去年为妙!~~~~

因为header函数本身就相当于<head>的意思!

比如:header("Content-type:image/png");
相当于:<meta http-equiv="Content-Type" content="image/png" />

但并不完全相同!
mrshelly 2005-05-30
  • 打赏
  • 举报
回复
下面这样试试~~~~~~~~
<? if ($_GET["action"]=="getimg"){
header("Content-type:image/png");
$img_handle = ImageCreate (230, 20) or die ("Cannot Create image");
$back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
$txt_color = ImageColorAllocate ($img_handle, 233, 114, 191);
ImageString ($img_handle, 31, 5, 5, "My first Program with GD", $txt_color);
ImagePNG ($img_handle);
}
else {
?>
<html>
<head>
<title>First program</title>
</head>
<body>
<img src="<? echo "$PHP_SELF?action=getimg";?>">
</body>
</html>
<?
}
?>
norsd 2005-05-30
  • 打赏
  • 举报
回复
To: viewpl("要学的太多..." 仰面观云而泣曰) ( )

好像也不行,输出如下:
塒NG IHDR ̄雨PLTE 閞縛?L贗DATx滃斜A酂MhF?!瀉?(刉偎&?憍侂擹厙衖'J璂??w?痐蟜Sc燙Ye烥丒龢?喃*T,(椽氿瘢Pe韡&H??&Ρ?殖?4I?ヶ砷孔慬!慽?1kg$;荘 E倶o櫠?{粭z鞣唴洛:`幘垂虜潹?砯S駜8煥?葠巾E弹E椽,襈幺@?&傴g晻G?'€?ijIEND瓸`?/body>
hityyp 2005-05-30
  • 打赏
  • 举报
回复
先输出header(),然后再html代码
norsd 2005-05-30
  • 打赏
  • 举报
回复
To : netvt(唯她(为了泡老婆,努力学习LISP)) ( ) 信誉:145

兄台你的这个方法,我试了一下,标题有了,但是图像没有了,变成了乱码<---其实是图像的数据
viewpl 2005-05-30
  • 打赏
  • 举报
回复
<?
ob_start()
?>
<html><head><title>First program</title></head><body><script language="php">header("Content-type:image/png");
$img_handle = ImageCreate (230, 20) or die ("Cannot Create image");
$back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
$txt_color = ImageColorAllocate ($img_handle, 233, 114, 191);
ImageString ($img_handle, 31, 5, 5, "My first Program with GD", $txt_color);
ImagePNG ($img_handle);
</script>
</body>
</html>
<?
ob_end_flush()
?>

试试看这样行不?

norsd 2005-05-30
  • 打赏
  • 举报
回复
再问一个,那我怎样设置网页title 那?
vitalgg 2005-05-30
  • 打赏
  • 举报
回复
去了就不是标准的HTML文件了!
<?php
header("Content-type:image/png");
?>
<html><head><title>First program</title></head><body>
<?php
$img_handle = ImageCreate (230, 20) or die ("Cannot Create image");
$back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
$txt_color = ImageColorAllocate ($img_handle, 233, 114, 191);
ImageString ($img_handle, 31, 5, 5, "My first Program with GD", $txt_color);
ImagePNG ($img_handle);
?>
</body>
</html>
norsd 2005-05-30
  • 打赏
  • 举报
回复
楼上给你100分,多谢了,实在是菜鸟我。晕
zhiin1 2005-05-30
  • 打赏
  • 举报
回复
header()函数之前没有有任何输出!

你把:<html><head><title>First program</title></head><body>去掉就行了!~
norsd 2005-05-30
  • 打赏
  • 举报
回复
我用的是 php5

21,890

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧