无法修改 PHP 中的标头信息


Cannot modify header information in PHP

当我运行此代码片段时:

<?php
$getposts = mysql_query("SELECT * FROM posts WHERE user_posted_to='$username' ORDER BY id DESC LIMIT 10") or die(mysql_error());
while ($row = mysql_fetch_assoc($getposts)) {
                        $id = $row['id'];
                        $body = $row['body'];   
                        $date_added = $row['date_added'];
                        $added_by = $row['added_by'];
                        $user_posted_to = $row['user_posted_to'];
                        $get_user_info = mysql_query("SELECT * FROM users WHERE username='$added_by'");
                                                $get_info = mysql_fetch_assoc($get_user_info);
                                                $profilepic_info = $get_info['profile_pic'];
                                                if ($profilepic_info == "") {
                                                 $profilepic_info = "./img/default_pic.jpg";
                                                }
                                                else
                                                {
                                                 $profilepic_info = "./userdata/profile_pics/".$profilepic_info;
                                                }
       echo "
                        <div style='float: left;'>
                        <img src='$profilepic_info' height='60'>
                        </div>
                        <div class='posted_by'>
                        Posted by:
                        <a href='$added_by'>$added_by</a> on $date_added</div>
                        <br /><br />$body<br /><br /><br /><br /><br />";
                        }
if (isset($_POST['sendmsg'])) {
 header("Location:send_msg.php?u=$username"); 
}

它输出以下警告:

Warning: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/socialnetwork/profile.php:27) in /opt/lampp/htdocs/socialnetwork/profile.php on line 30

为什么该代码没有重定向到msg_reply.php?如何使此代码工作?

您可以将ob_start();放在页面顶部,而不会留下任何空间。 <?php ob_start();?>