-1

I've already looked at a ton of fixes for this I've tried deleting whitespace but there was none, I've tried using the ob_start(); function but to no avail. Whatever I do it gives me this error.

Warning: Cannot modify header information - headers already sent by (output started at /home/gener105/public_html/header.php:37) in /home/gener105/public_html/includes/vault_post.inc.php on line 12

It says the output starts on the last line of my header.php file(Fig1) and it has a problem with me calling header(); in the function I need to use. This is because of me using the header(); function on line 12 in vault_post.inc.php(Fig2).

I'm just confused on why its doing this because theres no outputs before the the header is called.

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) The first function thats needed in the file

<?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"); 
    }
}

FORM THAT NEEDS THE FUNCTION

<?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'); ?>
cosmichero2025
  • 1,029
  • 4
  • 14
  • 37

1 Answers1

1

This is entirely obvious. And your code is entirely flawed and this is probably because you are using HTML form submission for the first time.

  1. Your header.php includes HTML text which is sent out immediately. So you want to call your Header() function, you need to make sure nothing is sent out before hand. You are having your session_start() on the first line of your code, this is correct and you need to make sure that this function is the first line of code in any PHP script processing that is using it. You need to be careful that no header info is sent before this function is called.

  2. HTMl form's action attribute is the url path, not the PHP function. <form action='form_process.php'>. You set the path here the same way you set link's href (relative or absolute path). Your form_process.php will then receive a $_POST global variable for you to handle your form data received. http://php.net/manual/en/reserved.variables.post.php

TurtleTread
  • 1,297
  • 2
  • 12
  • 23
  • So I should have the file path the .php file that has all the function but just get rid of the function and make it a if(isset) statement? – cosmichero2025 Apr 14 '17 at 05:23
  • Exactly. The script should actually DO the processing. – TurtleTread Apr 14 '17 at 05:29
  • Hey it worked! I'm new at this and I thought that it would be good to call individual functions guess I was wrong :D – cosmichero2025 Apr 14 '17 at 05:48
  • You can still break your codes into components/functions, you just need to actually invoke them for PHP to process them. Also did you understand the Header not sent problem. You'll need to understand that because sooner or later you will need to. – TurtleTread Apr 14 '17 at 05:51
  • If I understand it right it doesn't like it when echo,print or really any other output is used before it right? – cosmichero2025 Apr 14 '17 at 06:04
  • Yes because Header info is sent out first, once any actual content body is sent out, that means the HEADER info is already sent. session_start() also requires no content be sent before its call. – TurtleTread Apr 14 '17 at 06:13