-1

可能重复:
PHP 已发送的标头

我正在尝试创建一个基于会话的计数器。而且我的代码似乎不起作用。每当,我重新加载页面。输出是“你已经浏览了 0 次页面”。

    <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
 <?php
session_start();

if(!isset($_SESSION['count']))
    $_SESSION['count'] = 0;
else
    $_SESSION['count']++;

 echo "You Have Viewed this page ", $_SESSION['count'], " times.";  

 echo session_id(); ?>
</body>
</html>

什么时候,我在我的本地主机上运行这段代码。代码根本不起作用。当我在我的网络服务器(hostgator)上运行它时,我得到以下错误。

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/rohitink/public_html/uiet.org/s4/index.php:8) in /home/rohitink/public_html/uiet.org/s4/index.php on line 9

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/rohitink/public_html/uiet.org/s4/index.php:8) in /home/rohitink/public_html/uiet.org/s4/index.php on line 9
You Have Viewed this page 0 times.788141cfa1329643918f87f5f6c9eb5a

请给我有关如何修复此错误的详细信息。非常感谢你。

4

2 回答 2

1

您需要将 放在该session_start()页面的顶部:

<?php session_start(); ?>
<html>
....

原因是session_start()在客户端设置了cookie,而cookie只有在还没有输出的情况下才能设置。

于 2012-09-23T06:31:49.490 回答
1

你必须在页面的第一行开始会话,即使他的空间引起了警告

在此处输入图像描述

并且对于标头已经发送错误,请检查此标头已由 PHP 发送

于 2012-09-23T06:32:17.400 回答