在doctype之前PHP包含(),导致空格

Nev*_*sed 3 php whitespace include

我有一个有空白问题的网站.我在doctype之前包含了一些文件,这导致输出一个空格.

搜索后我找到了这个解决方案: doctype之前的空白问题

但是在删除后?>我仍然遇到问题.以下是代码:

<?php
include ("include/session.php");
include ("include/updatestage.php");                                        
?>
<!DOCTYPE HTML>....
Run Code Online (Sandbox Code Playgroud)

这是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'>");

}
Run Code Online (Sandbox Code Playgroud)

这是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' ");
Run Code Online (Sandbox Code Playgroud)

这是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'];
?>
Run Code Online (Sandbox Code Playgroud)

最后这里是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");          
?>
Run Code Online (Sandbox Code Playgroud)

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

好的,我已经添加了回来了?>并尝试了以下建议:

<!DOCTYPE HTML>
<?php
include("include/session.php");
include("include/updatestage.php");
?>
Run Code Online (Sandbox Code Playgroud)

或尝试:

<?php

echo "<!DOCTYPE HTML>\n";

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

?>
Run Code Online (Sandbox Code Playgroud)

生产:

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
Run Code Online (Sandbox Code Playgroud)

试:

<?php
include("include/session.php");
include("include/updatestage.php");
?><!DOCTYPE HTML>
Run Code Online (Sandbox Code Playgroud)

要么:

<?php

  ob_start();

  echo "<!DOCTYPE HTML>\n";

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

  ob_end_flush();

?>
Run Code Online (Sandbox Code Playgroud)

仍然产生白色空间.

Dav*_*dom 5

您可以像这样解决问题:

<?php
include ("include/session.php");
include ("include/updatestage.php");                                        
?><!DOCTYPE HTML>
Run Code Online (Sandbox Code Playgroud)

但是我已经观察到了这方面的错误行为(虽然不是自PHP4以来),所以保证修复的最简单的解决方案是将<!DOCTYPE>包含在之前 - 因为既没有包含文件输出任何东西,并且如果它确实会破坏你的文档.

<!DOCTYPE HTML>
<?php

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

?>
<!-- Rest of HTML -->
Run Code Online (Sandbox Code Playgroud)

或这个:

<?php

  echo "<!DOCTYPE HTML>\n";

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

?>
<!-- Rest of HTML -->
Run Code Online (Sandbox Code Playgroud)

请记住,如果使用第二个选项,请确保<开始<?php标记中的所有文件中的第一个字符.

编辑首先完全没有考虑到已经养成丑陋头脑的会话/ cookie问题,这是一个有效的解决方案:

<?php

  ob_start();

  echo "<!DOCTYPE HTML>\n";

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

  ob_end_flush();

?>
<!-- Rest of HTML -->
Run Code Online (Sandbox Code Playgroud)