在doctype之前PHP包含(),导致空格 [英] PHP include() before doctype, causing a white space

查看:107
本文介绍了在doctype之前PHP包含(),导致空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站存在空白问题。我在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>\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();

?>

仍然产生空格。

推荐答案

你可以解决这个问题:

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

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

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>\n";

  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.

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

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 -->

这篇关于在doctype之前PHP包含(),导致空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆