以下是我的网页的网页结构:
01.<?php // rnlogout.php
02.include_once 'rnheader.php';
03.echo "<h3>Log out</h3>";
04.
05.if (isset($_SESSION['user']))
06.{
07. destroySession(); // report error here!!!!!!
08. echo "You have been logged out. Please
09. <a href='index.php'>click here</a> to refresh the screen.";
10.}
11.else echo "You are not logged in";
12.?>
function destroySession()
{
$_SESSION=array();
if (session_id() != "" || isset($_COOKIE[session_name()]))
setcookie(session_name(), '', time()-2592000, '/');
session_destroy();
}
01.<?php // rnheader.php
02.include 'rnfunctions.php';
03.session_start();
04.
05.if (isset($_SESSION['user']))
06.{
07. $user = $_SESSION['user'];
08. $loggedin = TRUE;
09.}
10.else $loggedin = FALSE;
11.
12.echo "<html><head><title>$appname";
13.if ($loggedin) echo " ($user)";
14.
15.echo "</title></head><body><font face='verdana' size='2'>"; // called first!!!!!
16.echo "<h2>$appname</h2>";
17.
18.if ($loggedin)
19.{
20. echo "<b>$user</b>:
21. <a href='rnmembers.php?view=$user'>Home</a> |
22. <a href='rnmembers.php'>Members</a> |
23. <a href='rnfriends.php'>Friends</a> |
24. <a href='rnmessages.php'>Messages</a> |
25. <a href='rnprofile.php'>Profile</a> |
26. <a href='rnlogout.php'>Log out</a>";
27.}
28.else
29.{
30. echo "<a href='index.php'>Home</a> |
31. <a href='rnsignup.php'>Sign up</a> |
32. <a href='rnlogin.php'>Log in</a>";
33.}
34.?>
请看一下07行
destroySession(); // report error here
基于php手册,http://us2.php.net/setcookie
必须在脚本输出之前发送cookie。这里rnheader.php
甚至在函数destroySession中的setcookie调用之前调用echo。
那么如何更改页面结构以解决此问题?
The error message is as follows:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\LPMJ_examples\robinsnest\rnheader.php:12) in C:\xampp\htdocs\LPMJ_examples\robinsnest\rnfunctions.php on line 41
谢谢
答案 0 :(得分:1)
函数destroySession()必须在调用之前设置,所以将定义的函数放在<?php
之后的顶部。
答案 1 :(得分:1)
阅读PHP-Manual :: Output Control
如果您不想重写脚本,请使用输出控制功能(如上所述)
<?php
ob_start();
include_once 'rmheader.php';
echo '<h3>Log Out</h3>';
if (isset($_SESSION['user'])) {
destroySession(); // report error here!!!!!!
echo "You have been logged out. Please <a href='index.php'>click here</a> to refresh the screen.";
} else {
echo "You are not logged in";
}
function destroySession()
{
$_SESSION=array();
if (session_id() != "" || isset($_COOKIE[session_name()]))
setcookie(session_name(), '', time()-2592000, '/');
session_destroy();
}
ob_end_flush();
?>