NeverPhased
NeverPhased

Reputation: 1566

PHP include() before doctype, causing a white space

I have a website with a white space problem. I am including a few files before the doctype and this is causing a white space to be outputted.

After searching I have found this solution: Problem with whitespace before doctype

However after removing ?> I am still getting the problem. Below is the code:

<?php
include ("include/session.php");
include ("include/updatestage.php");                                        
?>
<!DOCTYPE HTML>....

Here is session.php:

<?php session_start();

// if a team name has been assigned to a session...
if ( isset($_SESSION ['teamName']))
{

//create "global variable $teamName that can be used across all pages as long as the function "session_start(); is present"
$teamName = $_SESSION ['teamName'];

}
else{

die ( "You are not logged in! Redirecting to login page...<meta http-equiv='REFRESH' content='2; url = index.php'>");

}

Here is updatestage.php:

<?php
include("config.php");
//update stage

$inserted="n";
mysql_query ("UPDATE `team` SET `inserted`='$inserted' WHERE teamName = '$teamName' ");

$advance="n";
mysql_query ("UPDATE `game` SET `advance`='$advance' ");

Here is getstageinfo.php:

<?php
include("include/config.php");
//Select the slogan from the current user
$currentStage = mysql_query("
SELECT `currentStage` FROM `team` WHERE `teamName` = '$teamName'
");
//assign the text located at the logo field (the path of the logo) to a variable $slogan
$row = mysql_fetch_assoc($currentStage);
$currentStage= $row['currentStage'];
?>

And finally here is config.php:

<?php
// connect to database
$con = mysql_connect("xxxxxxx","xxxxxxx","xxxxxxx"); 

if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

// select database 
mysql_select_db ("xxxxxxx");          
?>

//////////////////////////////////////////////////////////////////////////////////////////

Ok so I have added back in the ?> and tried the suggestions below:

<!DOCTYPE HTML>
<?php
include("include/session.php");
include("include/updatestage.php");
?>

Or trying:

<?php

echo "<!DOCTYPE HTML>\n";

include ("include/session.php");
include ("include/updatestage.php");

?>

Produces:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at public_html/period1.php:2) in public_html/include/session.php on line 1

Trying:

<?php
include("include/session.php");
include("include/updatestage.php");
?><!DOCTYPE HTML>

Or:

<?php

  ob_start();

  echo "<!DOCTYPE HTML>\n";

  include ("include/session.php");
  include ("include/updatestage.php");

  ob_end_flush();

?>

Still produces the white space.

Upvotes: 3

Views: 10783

Answers (5)

James Allan
James Allan

Reputation: 5

Open file in Notepadd++ From top bar: Encoding->Encode in UTF8 without BOM Save the file In Dreamweaver

Press CTRL + J or go to Change->Page Settings->Tile/Encoding and unchek UNICODE BOM if it is cheked.

Upvotes: 0

Jared
Jared

Reputation: 21

I figured it out. You have to encode "UTF-8 without BOM" for the file you are including.

The master php file doesn't necessarily have to encode with "UTF-8 without BOM", and in fact I'd recommend that you don't if you have certain special characters (caused problems for me).

Was also answered here: PHP include causes white space at the top of the page

Upvotes: 2

Ilmari Karonen
Ilmari Karonen

Reputation: 50328

OK, these steps should fix your problem:

  1. Go through all PHP files on your site. If the file ends with ?> (and possibly some whitespace), remove the ?>. (Of course, if there's some HTML text after the ?> then you shouldn't remove it.)

  2. While doing the above, also check that there's no whitespace in front of the first <?php in any files.

  3. If you still have problems after that, check for invisible characters in front of the <?php. There are several ways to do that, but looking at a hex dump of the files is one good way. The first two bytes of each PHP file (that doesn't begin with HTML text) should be <? (hex 3c 3f).

In the unlikely event that those steps won't solve the problem, let us know (and preferably include a link to a page where the problem occurs, so that we can check the output ourselves).

Upvotes: 1

DaveRandom
DaveRandom

Reputation: 88647

You may be able to fix the problem like this:

<?php
include ("include/session.php");
include ("include/updatestage.php");                                        
?><!DOCTYPE HTML>

But I have observed buggy behaviour in this respect (although not since PHP4), so the easiest solution that guarantees a fix is to put the <!DOCTYPE> before the include - since neither included file outputs anything, and would break your document if it did.

<!DOCTYPE HTML>
<?php

  include ("include/session.php");
  include ("include/updatestage.php");

?>
<!-- Rest of HTML -->

Or this:

<?php

  echo "<!DOCTYPE HTML>\n";

  include ("include/session.php");
  include ("include/updatestage.php");

?>
<!-- Rest of HTML -->

Remember to make sure that the < in the opening <?php tag is the first character in all files if you go with the second option.

EDIT Having in the first place completely failed to take into account the session/cookies problem that has reared it's ugly head, here is a working solution:

<?php

  ob_start();

  echo "<!DOCTYPE HTML>\n";

  include ("include/session.php");
  include ("include/updatestage.php");

  ob_end_flush();

?>
<!-- Rest of HTML -->

Upvotes: 5

user562854
user562854

Reputation:

You don't have to remove the ?>. Just the whitespace after it. Your code should look like this:

<?php
include ("include/session.php");
include ("include/updatestage.php");                                        
?><!DOCTYPE HTML>....

Upvotes: 2

Related Questions