【问题标题】:Cannot modify header information - headers already sent by [duplicate]无法修改标头信息 - [重复] 已发送标头
【发布时间】:2010-06-24 10:35:58
【问题描述】:
<div id="left-body-part-innerpage">
    <h1 class="mainheading">Contact Us</h1>
    <div id="contactus-right-div" class="content">
    <?php session_start();
        if( isset($_POST['button2']))           
        {
            if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) 
            {
                $name = $_POST['name'];
                // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
                session_destroy();
                header("Location: contactdb.php");
?>

我收到警告:session_start() [function.session-start]:无法发送会话缓存限制器 - 网站中的标头已发送(从网站开始输出)

警告:无法修改标头信息 - 标头已由网站中的(从网站开始的输出)发送

谁能帮帮我?

提前谢谢....

【问题讨论】:

  • 考虑包含他的文件,而不是重定向到它。
  • 会话警告呢?
  • 只需将 header 函数放在任何 HTML 之前。

标签: php session header


【解决方案1】:
  1. 启动会话需要设置 HTTP 标头
  2. 发送内容后无法发送标头
  3. &lt;?php 和 ?&gt; 之外的任何内容都构成内容(echo、print 等任何内容也是如此)

将您的会话代码移动到脚本的顶部。

【讨论】:

  • 嗨,大卫,我已将 session_start() 移至页面开头,但仍然收到相同的警告。
  • @Rachel 你把整个代码块移到页面的最开始了吗?
  • 信不信由你,header 函数还会发送不能出现在内容之后的标题。
  • 我无法移动代码 if( isset($_POST['button2'])) { if( $_SESSION['security_code'] == $_POST['security_code'] && !empty ($_SESSION['security_code'] ) ) 因为它用于检查验证码是否已正确填写。因为如果它被正确填写,那么只有用户应该被重定向到下一页。
  • @Rachel 您绝对可以而且必须将其放在页面顶部。输出 HTML 后,您将无法启动页面重定向。如果验证码不匹配,但 header() 命令 必须 在 amnything 输出之前出现,您可能需要稍微重写整个内容以显示错误消息。
【解决方案2】:

所有与会话、cookies、header() 等相关的工作(所有修改 http 标头的操作)都必须在第一次输出脚本之前完成...将您的 php 块放在标记之前

<?php session_start();
    if( isset($_POST['button2']))           
    {
        if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) 
        {
            $name = $_POST['name'];
            // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
            session_destroy();
            header("Location: contactdb.php");?>

<div id="left-body-part-innerpage">
<h1 class="mainheading">Contact Us</h1>
<div id="contactus-right-div" class="content">

【讨论】:

    【解决方案3】:

    作为一般规则,在开始输出内容之前先完成所有业务逻辑(例如会话管理)。

    如前所述,开始打印页面内容会自动发送页眉。

    【讨论】:

      【解决方案4】:

      警告:session_start() [function.session-start]:无法发送会话缓存限制器 - 网站中的标头已发送(从网站开始输出)

      Warning: Cannot modify header information - headers already sent by (output started at website) in website
      

      关于这两个问题:

      --- 在代码顶部使用会话开始。在此之前,您可以使用 ob_start() 来清除输出缓冲区。

      喜欢,

      <?php
      ob_start();
      session_start();
      ?>
      

      【讨论】:

        【解决方案5】:

        在发送标头之前使用输出缓冲来防止输出。

        <?php
        
        function callback($buffer) {
        
          // заменить все apples на oranges
          return (ereg_replace("apples", "oranges", $buffer));
        
        }
        
        ob_start("callback");
        //HERE you can send any headers you want. callback is not required here if you don't want
        ?>
        
        <html>
        <body>
        <p>It's like comparing apples to oranges.
        </body>
        </html>
        
        <?php
        //And here you can
        ob_end_flush();
        //If send headers here - you'll get warning
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-09
          • 2012-06-19
          • 1970-01-01
          • 2011-01-15
          • 1970-01-01
          • 2012-10-10
          相关资源
          最近更新 更多