PHP include() 在 doctype 之前,导致一个空格
我的网站存在空白问题.我在 doctype 之前包含了一些文件这导致输出空白.
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.
搜索后我找到了这个解决方案:doctype前的空格问题
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>....
这里是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");
?>
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
好的,所以我已经添加回 ?> 并尝试了以下建议:
Ok so I have added back in the ?> and tried the suggestions below:
<!DOCTYPE HTML>
<?php
include("include/session.php");
include("include/updatestage.php");
?>
或尝试:
<?php
echo "<!DOCTYPE HTML>
";
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>
";
include ("include/session.php");
include ("include/updatestage.php");
ob_end_flush();
?>
仍然产生空白.
推荐答案
你可以这样解决问题:
<?php
include ("include/session.php");
include ("include/updatestage.php");
?><!DOCTYPE HTML>
但是我观察到这方面的错误行为(尽管不是自 PHP4 以来),所以保证修复的最简单解决方案是将 <!DOCTYPE>
放在包含之前 - 因为两者都不包含file 输出任何内容,如果这样做会破坏您的文档.
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 -->
或者这个:
<?php
echo "<!DOCTYPE HTML>
";
include ("include/session.php");
include ("include/updatestage.php");
?>
<!-- Rest of HTML -->
记得确保<?php
标签中的<
是所有文件中的第一个字符,如果你去的话使用第二个选项.
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.
编辑首先完全没有考虑到会话/cookies问题已经引起了它的丑陋,这里是一个可行的解决方案:
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>
";
include ("include/session.php");
include ("include/updatestage.php");
ob_end_flush();
?>
<!-- Rest of HTML -->
相关文章