0

i need help again...

my PHP code:

<?php session_start();
include_once 'init.inc.php'; // connection to database
include_once 'testFunc.php'; 

Inside my testFunc.php is this:

<?php
function loggedIn(){

if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])){

    return true;
} else{

    return false;

}

} 

In my test1 is this:

<?php

if(loggedIn()){
?>
<a href="test.php">Home</a>|
<a href="testMessage.php">Messages</a>|
<a href="testLoggout.php">Log out</a>
<?php
}else{
    ?>
    <a href="test.php">Home</a> |
    <a href="testLogin.php">Log In</a>|
    <a href="testRegister.php">Register</a>
<?php
}
?>

include_once 'test1.php';
?>

// here starts the main Part.

<head> // here is line 8 in my code here it gives me the error.

</head>

 <body>

 <h3>Conversation - Message System</h3>
 <?php include_once 'testMessageBar.php' ?>
 <?php
 $my_id = $_SESSION['user_id'];
 ?>
 <br>

 <div>
 <?php

 if(isset($_GET['hash']) && !empty($_GET['hash'])){

    $hash = $_GET['hash'];
    $message_query = mysqli_query($db,"SELECT from_id, message FROM messages WHERE group_hash='$hash'");
    while($run_message = mysqli_fetch_array($message_query)){

            $from_id = $run_message['from_id'];
            $message = $run_message['message'];

        $user_query = mysqli_query($db, "SELECT username FROM users1 WHERE id='$from_id'");
        $run_user = mysqli_fetch_array($user_query);
        $from_username = $run_user['username'];

        echo "<p><b>$from_username</b><br/>$message</p>";
    }

    ?>
    <br/>
        <form method="post">
            <?php
            if(isset($_POST['message']) && !empty($_POST['message'])){
                $new_message = $_POST['message'];
                mysqli_query($db, "INSERT INTO messages VALUES('','$hash','$my_id','$new_message')");
                header('location: testConversation.php?hash='.$hash);

            }
            ?>
                Enter Message: <br/>
            <textarea name="message" rows="6" cols="30"></textarea>
            <br/></br>
            <input type="submit" value="Send Message">
        </form>
<?php

}else{

    echo "<b>Select Conversation </b>";
    $get_con = mysqli_query($db,"SELECT hash, user_one, user_two FROM message_group WHERE user_one='$my_id' OR user_two='$my_id'");
    while($run_con = mysqli_fetch_array($get_con)){
        $hash = $run_con['hash'];
        $user_one = $run_con['user_one'];
        $user_two = $run_con['user_two'];

        if($user_one == $my_id){

                $select_id = $user_two;
        }else{

            $select_id = $user_one;

        }
            $user_get = mysqli_query($db, "SELECT username FROM users1 WHERE id='$select_id'");
            $run_user = mysqli_fetch_array($user_get);
            $select_username = $run_user['username'];

                echo "<p><a href='testConversation.php?hash=$hash'>$select_username</a></p>";

    }
}

?>
</div>

</body>
</html>

I'm getting this error...

 Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/project/sites/test1.php:8)

I know that before header() there shouldn't be a output... but i don't know how i should fix this or where to position my header(); that everything is still working... i tried around now for couple hours still have no idea...

hope someone can help me out here :)

RedFire
  • 45
  • 5

1 Answers1

1

Look at where your header function is. It's in the middle of some HTML code.

By definition, you must send your headers before your body (in this case your HTML). It's just how the HTTP server response is structured.

Make sure any headers you send are done before your output even one character of your body.

I'm not sure why you are sending a Location header in the middle of a form. If you want to redirect the user to a new page, create a new question asking about how to redirect the user to a new page based on a certain event.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • I don't want to redirect the user to a new page. I redirect the user to the same page so the page refreshes... when i write something in the textarea and send it i have to refresh the page to show the content on the site because i get the message from the database.. don't know if it's understandable sorry :/ – RedFire Jan 09 '16 at 00:59
  • When you submit a form it automatically refreshes the page, so you don't need to write a header there. Just echo whatever you have retrieved from the database there. – Martin Konecny Jan 09 '16 at 01:25
  • Make sure you create a PHP endpoint to capture any form data sent when the user submits the form. Only at that point do you write to the database. See here http://www.w3schools.com/php/php_forms.asp – Martin Konecny Jan 09 '16 at 01:28
  • if i use echo i get the output but only when i refresh the page the 2nd time... when i submit the form the first time it dosen't echo any output. – RedFire Jan 09 '16 at 01:45
  • See here http://stackoverflow.com/questions/18920651/how-can-i-refresh-a-form-page-after-the-form-submits-to-blank – Martin Konecny Jan 09 '16 at 01:51
  • tried it with refreshing the page dosen't work either – RedFire Jan 09 '16 at 02:02