警告:无法修改标题信息Cant Fix

时间:2017-04-14 04:21:27

标签: php

我已经看过很多针对此问题的修复我尝试删除空格但没有,我尝试使用ob_start();功能但无济于事。无论我做什么,都会给我这个错误。

警告:无法修改标头信息 - 已发送的标头 (输出从/home/gener105/public_html/header.php:37开始)在第12行的/home/gener105/public_html/includes/vault_post.inc.php中

它说输出从我的header.php文件的最后一行开始(图1),我调用header()时出现问题;在我需要使用的功能中。这是因为我使用header();函数在vault_post.inc.php的第12行(图2)。

我只是对为什么这样做感到困惑,因为在调用标题之前没有输出。

FIG1(header.php)

<?php
session_start();
include 'includes/dbh.php';
include 'includes/vault_post.inc.php';
date_default_timezone_set('America/New_York');
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Generation Diary - Leave Them Something For Later</title>
    <!-- Bootstrap Core CSS -->
    <link href="lib/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom Fonts -->
    <link href="lib/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Cabin:700" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
    <!-- Theme CSS -->
    <link href="css/grayscale.min.css" rel="stylesheet">
    <!-- Temporary navbar container fix until Bootstrap 4 is patched -->
    <style>
        .navbar-toggler {
            z-index: 1;
        }

        @media (max-width: 576px) {
            nav > .container {
                width: 100%;
            }
        }
    </style>
</head>

FIG2(vault_post.inc.php)文件中需要的第一个函数

<?php
function setVaultPosts($conn) {

    if (isset($_POST['vault_sub'])) {
        $uid = $_POST['uid'];
        $date = $_POST['date'];
        $content = $_POST['content'];

            $sql = "INSERT INTO vaults (uid, date0, content) VALUES ('$uid', '$date', '$content')";
            $result = mysqli_query($conn, $sql);

            header("Location: http://www.generationdiary.com/user_vault.php?success"); 
    }
}

形成需要的功能

<?php include('header.php'); ?>
    <div class="container" id="vault_main">
        <div class="row row-offcanvas row-offcanvas-right">
            <div class="col-12 col-md-9">
                <p class="float-right hidden-md-up">
                    <button type="button" class="btn btn-primary btn-sm" data-toggle="offcanvas">Toggle nav</button>
                </p>
                <div class="jumbotron">
                    <h1 class="text-center">
                        <?php
                     if  (isset($_SESSION['username'])) {
                            echo $_SESSION['firstname'] . " " . $_SESSION['lastname'] . "'s";
                        } else {
                            echo "You are not logged in";
                        }

                    ?>

                    </h1>
                    <h2 class="text-center">Vault</h2> </div>
                <?php
                if  (isset($_SESSION['username'])) {
                   echo "
                   <form action='".setVaultPosts($conn)."' method='POST'>
                   <input type='hidden' name='uid' value='".$_SESSION['username']."'>
                    <input type='hidden' name='date' value='".date(' Y-m-d  ')."'>
                    <textarea class='ckeditor' name='content'></textarea>
                    <br>
                    <button class='btn btn-default btn-lg' type='submit' name='vault_sub'>Submit</button>
                    ";
                    getVaultPosts($conn);
                } else {
                    echo "log in";
                }

            ?>
            </div>
            <div class="col-6 col-md-3 sidebar-offcanvas" id="sidebar">
                <div class="fixed">
                    <div class="list-group"> <a href="#" class="list-group-item active">Vault</a> <a href="recipient.php" class="list-group-item">Recipient Settings</a> <a href="settings.php" class="list-group-item">Account Settings</a> <a href="includes/logout.inc.php" class="list-group-item">Log Out</a> </div>
                </div>
            </div>
        </div>
    </div>
<?php include('footer.php'); ?>

1 个答案:

答案 0 :(得分:1)

这是完全明显的。并且您的代码完全有缺陷,这可能是因为您第一次使用HTML表单提交。

  1. 您的header.php包含立即发送的HTML文本。所以你想调用你的Header()函数,你需要确保没有事先发出。您在代码的第一行上有session_start(),这是正确的,您需要确保此函数是使用它的任何PHP脚本处理中的第一行代码。在调用此函数之前,您需要注意不要发送头信息。

  2. HTMl表单的action属性是url路径,而不是PHP函数。 <form action='form_process.php'>。您在此处设置路径的方式与设置链接的href(相对路径或绝对路径)的方式相同。然后,您的form_process.php将收到一个$ _POST全局变量,以便您处理收到的表单数据。 http://php.net/manual/en/reserved.variables.post.php

相关问题