【问题标题】:PHP - exit(0) inside headers_sent()? [closed]PHP - 在 headers_sent() 内退出(0)? [关闭]
【发布时间】:2014-08-14 19:26:49
【问题描述】:

所以我有这个代码:

<?php
// Set headers
if (!headers_sent()) {
    header('Content-Type: text/html; charset=UTF-8');
    header('Content-Style-Type: text/css');
    header('Content-Script-Type: application/javascript');
    header('HTTP/1.1 200 OK');
    header('Content-language: en-US');
    header('X-Powered-By: ');
    header_remove('X-Powered-By');
    header('Last-Modified: Tue, 01 Jan 2013 00:00:00 GMT');
    header('Cache-Control: no-store, no-cache, max-age=0, must-revalidate');
    header('Pragma: no-store, no-cache, max-age=0, must-revalidate');
    header('Expires: Tue, 01 Jan 2013 00:00:00 GMT');
    exit(0);
}

添加exit(0); 后,我的页面完全空白。没有 HTML。任何想法为什么?我应该使用exit(0); 吗?它用于php.net 上的其中一个示例。此外,如果我删除exit(0),整个页面看起来还不错。

【问题讨论】:

  • exit() 退出,所以您只需发送标头
  • 所以你的意思是我不应该使用它?
  • 您应该在可以退出时使用(可能不仅仅是在发送标头之后)。
  • 可能在脚本文件的末尾?

标签: php if-statement http-headers exit


【解决方案1】:

您引用的示例是使用exit; 来停止页面的执行,因为页面正在通过标题重定向。你不想这样做,除非你想停止运行页面

通常,您不需要exit;,除非您提前停止您的页面。顺便说一句,headers_sent() 调用与这里并没有真正直接相关。

【讨论】:

  • OK 6 分钟后我会接受这个答案。另外,如果下面有更多代码,您的意思是 headers_sent() 不应使用 exit(0);。我刚刚把它放在我的&lt;/html&gt; 标签下。
  • @PowerUser 你根本不需要使用exit
  • @PowerUser 真的和headers_sent()没有关系。这只是在那里完成的任务的附带条件。而且,headers_sent() 根本没有“使用”它。最后,正确;如果您有下面要执行的代码,则不应使用exit;
猜你喜欢
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
  • 2018-02-21
  • 2017-08-07
  • 2011-04-24
  • 2020-05-06
  • 2015-12-20
  • 2012-07-13
相关资源
最近更新 更多