jjzjj

PHP include() before doctype,导致空白

coder 2024-04-25 原文

我的网站存在空白问题。我在文档类型之前包含了一些文件 这导致输出空白。

经过搜索我找到了这个解决方案: 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();

?>

仍然会产生空白。

最佳答案

您可以这样解决问题:

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

但我观察到这方面的错误行为(虽然不是从 PHP4 开始),所以保证修复的最简单解决方案是将 <!DOCTYPE>在 include 之前 - 因为两个 include 文件都没有输出任何东西,如果输出的话会破坏你的文档。

<!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如果您选择第二个选项,标记是所有文件中的第一个字符。

编辑 首先完全没有考虑到已经引起它丑陋头脑的 session /cookies 问题,这是一个可行的解决方案:

<?php

  ob_start();

  echo "<!DOCTYPE HTML>\n";

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

  ob_end_flush();

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

关于PHP include() before doctype,导致空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9131265/

有关PHP include() before doctype,导致空白的更多相关文章

  1. Ruby 守护进程导致 ActiveRecord 记录器 IOError - 2

    我目前正在用Ruby编写一个项目,它使用ActiveRecordgem进行数据库交互,我正在尝试使用ActiveRecord::Base.logger记录所有数据库事件具有以下代码的属性ActiveRecord::Base.logger=Logger.new(File.open('logs/database.log','a'))这适用于迁移等(出于某种原因似乎需要启用日志记录,因为它在禁用时会出现NilClass错误)但是当我尝试运行包含调用ActiveRecord对象的线程守护程序的项目时脚本失败并出现以下错误/System/Library/Frameworks/Ruby.frame

  2. ruby-on-rails - 如何为空白字段编写 rspec? [Rails3.1] - 2

    我使用rails3.1+rspec和factorygirl。我对必填字段(validates_presence_of)的验证工作正常。我如何让测试将该事实用作“成功”而不是“失败”规范是:describe"Addanindustrywithnoname"docontext"Unabletocreatearecordwhenthenameisblank"dosubjectdoind=Factory.create(:industry_name_blank)endit{shouldbe_invalid}endend但是我失败了:Failures:1)Addanindustrywithnona

  3. ruby - 从另一个私有(private)方法中使用 self.xxx() 调用私有(private)方法 xxx,导致错误 "private method ` xxx' called” - 2

    我正在尝试获得良好的Ruby编码风格。为防止意外调用具有相同名称的局部变量,我总是在适当的地方使用self.。但是现在我偶然发现了这个:classMyClass上面的代码导致错误privatemethodsanitize_namecalled但是当删除self.并仅使用sanitize_name时,它会起作用。这是为什么? 最佳答案 发生这种情况是因为无法使用显式接收器调用私有(private)方法,并且说self.sanitize_name是显式指定应该接收sanitize_name的对象(self),而不是依赖于隐式接收器(也是

  4. ruby - 为什么 return 关键字会导致我的 'if block' 出现问题? - 2

    下面的代码工作正常:person={:a=>:A,:b=>:B,:c=>:C}berson={:a=>:A1,:b=>:B1,:c=>:C1}kerson=person.merge(berson)do|key,oldv,newv|ifkey==:aoldvelsifkey==:bnewvelsekeyendendputskerson.inspect但是如果我在“ifblock”中添加return,我会得到一个错误:person={:a=>:A,:b=>:B,:c=>:C}berson={:a=>:A1,:b=>:B1,:c=>:C1}kerson=person.merge(berson

  5. ruby-on-rails - 什么会导致与 APNS 的连接间歇性断开连接? - 2

    我有一个ruby​​脚本可以打开与Apple推送服务器的连接并发送所有待处理的通知。我看不出任何原因,但当Apple断开我的脚本时,我遇到了管道损坏错误。我已经编写了我的脚本来适应这种情况,但我宁愿只是找出它发生的原因,这样我就可以在第一时间避免它。它不会始终根据特定通知断开连接。它不会以特定的字节传输大小断开连接。一切似乎都是零星的。您可以在单个连接上发送的数据传输或有效负载计数是否有某些限制?看到人们的解决方案始终保持一个连接打开,我认为这不是问题所在。我看到连接在3次通知后断开,我看到它在14次通知后断开。我从未见过它能超过14点。有没有人遇到过这种类型的问题?如何处理?

  6. ruby - 在多个线程中引用类方法会导致自动加载循环依赖崩溃 - 2

    代码:threads=[]Thread.abort_on_exception=truebegin#throwexceptionsinthreadssowecanseethemthreadseputs"EXCEPTION:#{e.inspect}"puts"MESSAGE:#{e.message}"end崩溃:.rvm/gems/ruby-2.1.3@req/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:478:inload_missing_constant':自动加载常量MyClass时检测到循环依赖稍加研究后,

  7. ruby - 返回空白页的最小 Capybara/Poltergeist 测试 - 2

    看来我正在回顾SO帖子中采取的步骤:Capybara,PoltergeistandPhantomjsandgivinganemptyresponseinbody.(如果你愿意,可以将其标记为重复,但我包含了一个最小的独立测试用例和版本号。)问题我做错了什么吗?我可以运行另一个可能有助于隔离问题的最小测试吗?文件:pgtest.rbrequire'rubygems'require'capybara'require'capybara/dsl'require'capybara/poltergeist'modulePGTestincludeCapybara::DSLextendselfdeft

  8. Ruby:为什么等号在文字正则表达式中会导致解析错误? - 2

    这些解析和执行良好:"=".scan(/=/)"=".scan(/=/)这会导致“未终止的正则表达式遇到文件结尾”:"=".scan/=/如果我在=之前插入一些内容,错误就会消失:"=".scan/^=/这是怎么回事? 最佳答案 我猜你正在点击thisintheparser:case'/':if(IS_BEG()){lex_strterm=NEW_STRTERM(str_regexp,'/',0);returntREGEXP_BEG;}if((c=nextc())=='='){set_yylval_id('/');lex_state

  9. ruby - 使用 watir-webdriver 打开多个线程导致 'Connection refused' 错误 - 2

    我有这个简单的例子:require'watir-webdriver'arr=[]sites=["www.google.com","www.bbc.com","www.cnn.com","www.gmail.com"]sites.eachdo|site|arr每次我运行这个脚本,我都会得到ruby/2.1.0/net/http.rb:879:in`initialize':Connectionrefused-connect(2)for"127.0.0.1"port9517(Errno::ECONNREFUSED)或者其中一个浏览器在至少一个线程上意外关闭。另一方面,如果我在每个循环周期结束

  10. ruby-on-rails - Rails 不呈现 public/index.html 文件;浏览器中的空白页面 - 2

    当我将我的Rails+React应用程序部署到Heroku时,我遇到了问题。React客户端位于Rails应用程序的client/目录中。由于使用了react-router,Rails服务器需要知道从React构建中渲染index.html。当我在Heroku上部署客户端时,脚本将内容从client/build/.复制到Rails应用程序的public/目录。现在问题来了:当我的路由检测到类似example.com/about的路径时,它会尝试呈现public/index.html。方法如下:deffallback_index_htmlrenderfile:"public/index.

随机推荐