2017-04-14 140 views
-1

我已经看了很多修复这个我试过删除空白,但没有,我试过使用ob_start();功能但无济于事。无论我做什么,都会给我这个错误。警告:无法修改标头信息无法修复

警告:不能更改头信息 - 已经 发送的报头/home/gener105/public_html/includes/vault_post.inc.php(输出开始/home/gener105/public_html/header.php:37)上线12

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

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

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'); ?> 
+0

你在哪里调用这个函数'setVaultPosts' –

+0

你能告诉那里的header.php叫? – Hmmm

+0

我打算在具有想要使用此功能的表单在本地服务器上运行的PHP页面调用它。 – cosmichero2025

回答

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

+0

因此,我应该有.php文件的文件路径,它具有所有的功能,但只是获取摆脱这个函数,并使其成为一个if(isset)语句? – cosmichero2025

+0

准确地说,脚本实际上应该执行处理。 – TurtleTread

+0

嘿它工作!我是新手,我认为可以称个别函数是错误的:D – cosmichero2025

相关问题