2

可能重复:
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);结果相同..

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

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

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

4

1 回答 1

0

启用error_reporting(E_ALL);并查看它的内容。删除?>您的尾随形式bamfg_functions.php- 如果这可以解决您的问题,那么您在?>. 如果问题仍然存在,您可以通过启用输出缓冲来解决它(但是您应该确定它并修复甚至“解决”问题的解决方法)。只需添加ob_start();脚本的第一行即可。

你好像也用global错了。global不是声明全局变量。就是让全局变量在方法/类/函数范围内可见。因此,例如,这段代码没有多大意义:

$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'];
}
于 2012-11-17T18:10:20.093 回答