2012-02-03 27 views
3

我有一个网站有一个空白问题。我在doctype 之前包含了一些文件,这会导致输出一个空白区域。PHP包含()doctype之前,导致一个空白区域

搜索后,我发现这个解决方案: Problem with whitespace before doctype

但是除去?>后,我仍然得到这个问题。下面是代码:

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

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

} 

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

这里是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']; 
?> 

的ð终于在这里是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");   
?> 

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

好了,所以我在加回>和尝试以下建议:

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

或尝试:

<?php 

echo "<!DOCTYPE HTML>\n"; 

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

?> 

产地:

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 

尝试:

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

或者:

<?php 

    ob_start(); 

    echo "<!DOCTYPE HTML>\n"; 

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

    ob_end_flush(); 

?> 

仍然生产的空白。

+0

将'session.php'和'updatestage.php'复制并粘贴到纯文本编辑器(如记事本)中,然后将包复制并粘贴到文档中。另外,确保在这两个文件中''?php'开始之前或结束'?>'之后没有空格。 – Dutchie432 2012-02-03 15:41:26

+0

发现问题,它是嵌套'include(“config.php”);'而不是包括我刚刚将代码粘贴到'updatestage.php'中 – NeverPhased 2012-02-03 15:42:41

+1

那么问题可能是confuig中的空白。php' – Dutchie432 2012-02-03 15:43:21

回答

5

您可能能够解决这样的问题:

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

但我已经在这方面观察到的错误行为(尽管不是因为PHP4),所以保证修复的最简单的解决方案是在包含之前放置<!DOCTYPE> - 因为这两个包含的文件都不输出任何内容,并且如果它包含文件则会破坏文件。

<!DOCTYPE HTML> 
<?php 

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

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

或者这样:

<?php 

    echo "<!DOCTYPE HTML>\n"; 

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

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

记住要确保在开放<?php标签<第一字符中的所有文件,如果你使用第二个选项去。

编辑有摆在首位完全没有考虑到已经饲养它丑陋的头会话/饼干的问题,这里是一个有效的解决方案:

<?php 

    ob_start(); 

    echo "<!DOCTYPE HTML>\n"; 

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

    ob_end_flush(); 

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

请参阅我的编辑 – NeverPhased 2012-02-03 16:04:35

+0

我已添加config.php也 – NeverPhased 2012-02-03 16:06:51

+0

@ user1064028请参阅我上面的编辑;-) – DaveRandom 2012-02-03 16:07:48

2

您不必删除?>。后面只有空白。您的代码应该是这样的:

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

如果你不打算从PHP文件输出HTML,那么你应该忽略'?>'。 – 2012-02-03 15:55:26

+3

但是,正如你所看到的,在这种情况下,HTMl恰好在它之后 – technology 2012-02-03 16:07:46

+1

在主脚本中,是的。但不包括他所包含的_other_ PHP文件,这是实际问题所在。 – 2012-02-03 16:27:28

1

OK,这些步骤应该解决您的问题:

  1. 去走遍所有 PHP文件在您的网站。如果文件以?>(可能还有一些空格)结尾,请删除?>。虽然做上述(当然,如果?>后有一些HTML文本,那么你不应该将其删除。)

  2. ,还要检查有在任何文件中的第一<?php前面没有空格。

  3. 如果之后仍然有问题,请检查<?php前面的隐形字符。有几种方法可以做到这一点,但查看文件的hex dump是一种好方法。每个PHP文件的前两个字节(不以HTML文本开头)应该是<?(十六进制3c 3f)。

万一,这些措施不会解决这个问题,让我们知道(最好包括一个链接到发生问题,这样我们就可以检查输出自己的一页)。

+0

感谢我可以与你联系吗? – NeverPhased 2012-02-03 16:40:36

1

我想通了。您必须为文件包括编码“没有BOM的UTF-8”。

master php文件不一定要用“UTF-8 without BOM”进行编码,事实上,如果您有某些特殊字符(对我造成问题),我建议您不要。

也是在这里找到答案:PHP include causes white space at the top of the page

0

在Notepadd ++ 打开文件从最上面一栏:Encoding->编码在UTF8无BOM 保存文件 在Dreamweaver

按CTRL + J或去更改 - >页面设置 - >平铺/编码,并取消选中UNICODE BOM。