有require_once(xyz.php);破坏我的标题重定向,我做错了什么



可能的重复项:
PHP中的"警告:标头已发送"

我的bamfg_functions.php代码:

<?php
error_reporting(E_ALL & ~E_NOTICE);
define('THIS_SCRIPT', 'test');
define('CSRF_PROTECTION', true); 
require_once('global.php');
function bamfg_navigation()
{
global $vbulletin;
if ($vbulletin->userinfo['userid']) {
$navigation .= "<center>- Browse Vehicles - <a href='./bamfg.php'>Search  Vehicles</a>     -<br>- <a href='./bamfg_vehicle.php'>ADD / EDIT Vehicles</a><br><br>      </center>";
}
else {
$navigation .= "<center>- Browse Vehicles - <a href='./bamfg.php'>Search Vehicles</a>  -<br><br></center>";
}
return $navigation;
}
?>

这就是我将bamfg_functions.php包含在bamfg_vehicle.php文件中的方式。

require('bamfg_functions.php');
bamfg_navigation = bamfg_navigation();
global $bamfg_navigation;

这是我在bamfg_vehicle.php内受影响的许多do==语句之一。

if($_REQUEST['do'] == 'add_comment') {
$userid = $vbulletin->userinfo['userid'];
$username = $vbulletin->userinfo['username'];
$vbulletin->input->clean_gpc('r', 'id', TYPE_INT);
$vehicle_id = $vbulletin->GPC['id'];
$owner_userid = $_GET["owner_userid"];
// ** THIS GETS THE "POSTED" INFORMATION FROM THE PAGE AND CONVERTS TO VARIABLE - CLEANS INPUT
$vbulletin->input->clean_array_gpc('p', array(
'comment' => TYPE_NOHTML,
));  
$comment = $vbulletin->GPC['comment'];

// MAKES SURE THE COMMENT ISN'T BLANK
if (strlen($comment) == 0){
Header( "Location: $website_url/bamfg_vehicle.php?do=view_vehicle&    id=$vehicle_id" );
}
else {
$sql = "INSERT INTO ". TABLE_PREFIX ."BAMFG_comment (
  comment_id, 
  vehicle_id, 
  userid,
  owner_userid, 
  username,
  comment) VALUES (
  '". $comment_id ."',
  '". $vehicle_id ."',
  '". $userid ."',
  '". $owner_userid ."',
  '". $username ."',
  '". $comment ."')";
$db->query_write($sql);
Header( "Location: $website_url/bamfg_vehicle.php?do=view_vehicle&id=$vehicle_id" );
}}

我的问题是当我"需要"bamfg_functions.php文件时,它会破坏我所有的标头重定向,我也尝试过require_once(bamfg_functions.php); 并且只包括(bamfg_functions.php); 具有相同的结果。

一旦我注释掉调用文件的行,标题就会重定向工作,这让我发疯了。

我意识到标头重定向仅在调用之前没有数据输出到浏览器时才有效,但我在任何地方都看不到?

任何建议都会很棒,谢谢。

启用error_reporting(E_ALL);,看看它能说明什么。删除尾随?>形成您的bamfg_functions.php - 如果这解决了您的问题,那么您在 ?> 之后有空格。如果问题仍然存在,您可以通过启用输出缓冲来解决它(但是您应该确定它并修复甚至解决方法"解决"问题)。只需添加ob_start();作为脚本的第一行即可。

你似乎也用错了globalglobal不是声明变量全局。它是为了使全局变量在方法/类/函数范围内可见。因此,例如,这段代码没有多大意义:

$a = "foo";
global $a;
function b() {
  echo $a;
}

虽然这是"更好的"(引用,因为使用global总是不好的):

$a = "foo";
function b() {
  global $a;
  echo $a;
}

但即使你确定你需要global(即如果你不能对代码进行这么多返工),你仍然不应该使用global而是访问$_GLOBALS[]。所以这是最好的坏:

$a = "foo";
function b() {
  echo $_GLOBALS['a'];
}

最新更新