关于Cookie的问题,大虾帮忙!

wdman 2003-12-29 11:58:23
我的代码如下:
<html>
<head>
<?php
$CuID="hehe";
setcookie("UserID",$CuID);
?>
</head>
<body>
</body>
</html>

报错如下:
Warning: Cannot modify header information - headers already sent by (output started at H:\Apache\Apache2\htdocs\php\testSession.php:5) in H:\Apache\Apache2\htdocs\php\testSession.php on line 7

我把那段代码放到<body>里面也是这样警告,不知道为什么,大虾帮忙!
...全文
70 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mikespook 2003-12-30
  • 打赏
  • 举报
回复
ob_start
(PHP 4 )

ob_start -- Turn on output buffering
Description
void ob_start ( [string output_callback])


This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents.

An optional output_callback function may be specified. This function takes a string as a parameter and should return a string. The function will be called when ob_end_flush() is called, or when the output buffer is flushed to the browser at the end of the request. When output_callback is called, it will receive the contents of the output buffer as its parameter and is expected to return a new output buffer as a result, which will be sent to the browser.

注: In PHP 4.0.4, ob_gzhandler() was introduced to facilitate sending gz-encoded data to web browsers that support compressed web pages. ob_gzhandler() determines what type of content encoding the browser will accept and will return it's output accordingly.

Output buffers are stackable, that is, you may call ob_start() while another ob_start() is active. Just make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.

ob_end_clean(), ob_end_flush(), ob_clean(), ob_flush() and ob_start() may not be called from a callback function. If you call them from callback function, the behavior is undefined. If you would like to delete the contents of a buffer, return "" (a null string) from callback function.

例子 1. User defined callback function example

<?php

function callback($buffer) {

// replace all the apples with oranges
return (ereg_replace("apples", "oranges", $buffer));

}

ob_start("callback");

?>

<html>
<body>
<p>It's like comparing apples to oranges.
</body>
</html>

<?php

ob_end_flush();

?>



Would produce:


<html>
<body>
<p>It's like comparing oranges to oranges.
</body>
</html>

手册上抄的~
episome 2003-12-30
  • 打赏
  • 举报
回复
ob_start() 将输出暂时放到缓冲区,不输出
aoenzh 2003-12-29
  • 打赏
  • 举报
回复
肯定在这段程序前你有输出语句
在setcookie是不也许的
<title>也不行
episome 2003-12-29
  • 打赏
  • 举报
回复
<?
ob_start();
?>
<html>
<head>
<?php
$CuID="hehe";
setcookie("UserID",$CuID);
?>
</head>
<body>
</body>
</html>
feyge 2003-12-29
  • 打赏
  • 举报
回复
<?php
$CuID="hehe";
setcookie("UserID",$CuID);
?>
<html>
<head>
</head>
<body>
</body>
</html>


使用cookie、session、header,要保证之前没有任何html语句的输出
wdman 2003-12-29
  • 打赏
  • 举报
回复
马上揭帖!
wdman 2003-12-29
  • 打赏
  • 举报
回复
谢谢各位!
to:episome(3ants.org)
ob_start()是什么意思?
feel8 2003-12-29
  • 打赏
  • 举报
回复
setcookie前有输出,二楼,三楼的方法都可以

21,882

社区成员

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

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