'Header()' 和 'if(isset($_SESSION...then echo $_SESSION.. ' 函数发生冲突并发出警告:header 已发送
'Header()' and 'if(isset($_SESSION...then echo $_SESSION.. ' functions get in conflict and throw a warning: header was already sent
我有这个令人困惑的警告消息,我不知道如何解决,因为我不知道是什么问题。我有我的 videoator-a.php 文件,其中这行代码:
<?php require_once "esse.php";?>
<?php
require("common.php");
if(empty($_SESSION['username']))
{
header("Location: index.php");
}
?>
与包含文件中的代码行冲突 esse.php:
<option><?php if(isset($_GET['lang'])){echo $_GET['lang'];}else{?>Language<?php } ?></option>
请注意 esse.php 中有一行:
session_start();
(只是认为这可以帮助)
当我打开 videator-a.php 页面时,它会发出警告:
Warning: Cannot modify header information - headers already sent by (output started at D:\wamp\www\pollo\esse.php:118) in D:\wamp\www\pollo\videator-a.php on line 6
第 6 行有:
header("Location: index.php");
在 esse.php 的第 118 行有:
<option><?php if(isset($_GET['lang'])){echo $_GET['lang'];}else{?>Language<?php } ?></option>
如果我像这样修改我的 videator-a.php 文件:
<?php
require("common.php");
header("Location: index.php");
?>
<?php require_once "esse.php";?>
警告消失。为什么我不能检查两个 php 文件中是否存在 session?这里出了什么问题? (不好意思,我有点烧水壶)
我的问题是 - 为什么会发生错误。因为它说 - header 已经发送 - 但我没有在 esse.php 文件中发送 ny header。仅检查 session 是否存在以及是否存在 - 回显它。
请帮我解决问题。
提前致谢。
使用 ob_start();
解决问题
基本上先放在每个文件里
if(!ob_get_level()) {
ob_start();
register_shutdown_function(function(){
if(ob_get_level()) ob_end_flush();
});
}
但这个话题有点复杂,请在此处阅读更多内容
What's the use of ob_start() in php?
whats the difference between ob_flush and ob_end_flush?
我有这个令人困惑的警告消息,我不知道如何解决,因为我不知道是什么问题。我有我的 videoator-a.php 文件,其中这行代码:
<?php require_once "esse.php";?>
<?php
require("common.php");
if(empty($_SESSION['username']))
{
header("Location: index.php");
}
?>
与包含文件中的代码行冲突 esse.php:
<option><?php if(isset($_GET['lang'])){echo $_GET['lang'];}else{?>Language<?php } ?></option>
请注意 esse.php 中有一行:
session_start();
(只是认为这可以帮助) 当我打开 videator-a.php 页面时,它会发出警告:
Warning: Cannot modify header information - headers already sent by (output started at D:\wamp\www\pollo\esse.php:118) in D:\wamp\www\pollo\videator-a.php on line 6
第 6 行有:
header("Location: index.php");
在 esse.php 的第 118 行有:
<option><?php if(isset($_GET['lang'])){echo $_GET['lang'];}else{?>Language<?php } ?></option>
如果我像这样修改我的 videator-a.php 文件:
<?php
require("common.php");
header("Location: index.php");
?>
<?php require_once "esse.php";?>
警告消失。为什么我不能检查两个 php 文件中是否存在 session?这里出了什么问题? (不好意思,我有点烧水壶)
我的问题是 - 为什么会发生错误。因为它说 - header 已经发送 - 但我没有在 esse.php 文件中发送 ny header。仅检查 session 是否存在以及是否存在 - 回显它。 请帮我解决问题。 提前致谢。
使用 ob_start();
解决问题基本上先放在每个文件里
if(!ob_get_level()) {
ob_start();
register_shutdown_function(function(){
if(ob_get_level()) ob_end_flush();
});
}
但这个话题有点复杂,请在此处阅读更多内容
What's the use of ob_start() in php?
whats the difference between ob_flush and ob_end_flush?