PHP include() 在 doctype 之前,导致空格
我的网站存在空白问题。我在文档类型之前包含了一些文件 这会导致输出空白。
经过搜索我找到了这个解决方案: 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();
?>
仍然产生空白。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您也许可以像这样解决问题:
但是我观察到这方面的错误行为(尽管从 PHP4 开始就没有),因此保证修复的最简单的解决方案是放置
在包含之前 - 因为包含的文件都不会输出任何内容,并且如果输出的话会破坏您的文档。
或者这样:
如果您选择,请记住确保开始
标记中的
<
是所有文件中的第一个字符与第二个选项。编辑首先完全没有考虑到会话/cookies问题,这已经引起了它的丑陋,这里有一个可行的解决方案:
You may be able to fix the problem like this:
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.Or this:
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:
您不必删除
?>
。只是后面有一个空格。您的代码应如下所示:You don't have to remove the
?>
. Just the whitespace after it. Your code should look like this:我想通了。您必须对要包含的文件进行“UTF-8 without BOM”编码。
主 php 文件不一定必须使用“UTF-8 without BOM”进行编码,事实上,如果您有某些特殊字符(给我带来了问题),我建议您不要这样做。
也在这里得到了回答:PHP包含导致空格在页面顶部
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
好的,这些步骤应该可以解决您的问题:
浏览您网站上的所有 PHP 文件。如果文件以
?>
结尾(可能还有一些空格),请删除?>
。 (当然,如果?>
之后有一些 HTML 文本,那么您不应该将其删除。)在执行上述操作时,还要检查第一个 < 前面没有空格。 code> 在任何文件中。
如果之后仍有问题,请检查
前面是否有不可见字符。有多种方法可以做到这一点,但查看文件的十六进制转储是一种好方法。每个 PHP 文件(不以 HTML 文本开头)的前两个字节应为
(十六进制
3c 3f
)。万一这些步骤无法解决问题,请告诉我们(最好包含出现问题的页面的链接,以便我们可以自己检查输出)。
OK, these steps should fix your problem:
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.)While doing the above, also check that there's no whitespace in front of the first
<?php
in any files.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<?
(hex3c 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).
在 Notepad++ 中打开文件
从顶部栏:编码 -> 以无 BOM 的 UTF8 编码
保存文件
在 Dreamweaver 中
按 CTRL + J 或转到“更改”->“页面设置”->“平铺/编码”,然后取消选中“UNICODE BOM”(如果已选中)。
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.