2

Possible Duplicate:
“Warning: Headers already sent” in PHP

My bamfg_functions.php code:

<?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;
}
?>

This is how I am including the bamfg_functions.php into my bamfg_vehicle.php file.

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

Here is one of my many do== statements within bamfg_vehicle.php that is effected..

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" );
}}

My issue is when I "require" the bamfg_functions.php file it breaks all my header redirects, I've also tried require_once(bamfg_functions.php); and just include(bamfg_functions.php); with the same result..

As soon as I comment out the line that calls the file, the header redirects work and it's driving me nuts..

I realize the header redirects only work if there is no data outputted to the browser before it is called, but I don't see that anywhere?

Any advice would be awesome, thanks..

Community
  • 1
  • 1
Dusty Joe
  • 51
  • 4
  • 1
    See [Byte-Order Mark](http://stackoverflow.com/questions/2558172/utf-8-bom-signature-in-php-files). – DCoder Nov 17 '12 at 18:07
  • what if you remove error reporting? – d-_-b Nov 17 '12 at 18:07
  • Usual way to track down a problem like this is to cut out half of the code and see if the problem persists. If it *does*, then the problem is in the half of the code you kept in. If it *doesn't*, then the problem *may* be in the half you removed (swap halves and see if you swap the result). If you've reduced the problem by half, do it again. If the swap procedure failed, try removing trailing/following parts of the code (the problem seems to span the half-way boundary) – Damien_The_Unbeliever Nov 17 '12 at 18:21
  • And the winner was... trailing spaces after the ?>.. I feel like a tard.. As for the rest of the code, please keep the suggestions coming and I will take all of it to heart. I've been self teaching myself for about a year now with the help of Google and looking through code and trying to understand what does what where.. Thanks so much guys. – Dusty Joe Nov 17 '12 at 18:31

1 Answers1

0

Enable error_reporting(E_ALL); and see what it tells. Remove trailing ?> form your bamfg_functions.php - if that fixes your problem, then you had whitespaces after ?>. If problem still occurs, you can work it around (however you should nail it down and fix even workaround "solves" the problem) by enabling output buffering. Just add ob_start(); as very first line of you script.

You also seem to use global wrong. global is NOT to declare variable global. It is to make global variable visible in i.e. method/class/function scope. So for example this code makes no much sense:

$a = "foo";
global $a;

function b() {
  echo $a;
}

while this is "better" (quoted, as using global is always bad):

$a = "foo";

function b() {
  global $a;
  echo $a;
}

but even if you sure you need global (i.e. if you cannot rework the code so much) you still should NOT use global but access $_GLOBALS[] instead. So this is the best of bad:

$a = "foo";

function b() {
  echo $_GLOBALS['a'];
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I don't recommend output buffering, but I'm almost certain the problem is trailing whitespace in the other file. – Explosion Pills Nov 17 '12 at 18:12
  • I do not recommend either (which I clearly stated). I am just giving certain hints to where to look at/for as the OP's question is not precise enough and he facing one of this common issue almost for sure. – Marcin Orlowski Nov 17 '12 at 18:15