PHP - cookie和标题位置:无法修改标题信息 - 已由[重复]发送的标题

This question already has an answer here:

This is my user log in page. After the user enter log in details, I want to store cookie files and direct the user to home page. However, I keep getting this error I have tried every possible way to solve it and I have read other questions posted on here but nothing did solve my problem... am I missing something here? what could it be?

<?php //<--- line 4

error messages:

Warning: Cannot modify header information - headers already sent by (output started at /www/99k.org/p/h/o/phoneclassmate/htdocs/login.php:4) in /www/99k.org/p/h/o/phoneclassmate/htdocs/login.php on line 6
</div>

You need to enclose your if statement block as shown below:

if($_POST['submit']) {
    $hour = time() + 3600;
    setcookie('username', $_POST['username'], $hour,'/');
    setcookie('password', $_POST['pass'], $hour,'/');
    header("Location: tutorhomepage.php");
}

Also, you need to move connection etc. part just after the above segment.

<?php

if($_POST['submit']){
    $hour = time() + 3600;
    setcookie('username', $_POST['username'], $hour,'/');
    setcookie('password', $_POST['pass'], $hour,'/');
    header("Location: tutorhomepage.php");
}
//------ server and mysql connection
$connection = mysql_connect('mywebsite', 'username', 'password') or die('Unable to connect');
mysql_select_db('myDB') or die('unbable to select DB');
//------log in cookie check
if (isset($_COOKIE['username'])) {
    $username = $_COOKIE['username'];
    $pass = $_COOKIE['password'];
    $check = mysql_query("SELECT * FROM tutors WHERE username = '$username'") or die(mysql_error());
    while ($info = mysql_fetch_row($check)) {
        if ($pass != $info[1]) {    
        } else {
            header("Location: /tutorhomepage.php");
        }
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login</title>
    </head>    
    <body><?php
//------- if form is submitted
if (isset($_POST['submit'])) {
    // check if the user enetered login details
    if (!$_POST['username'] | !$_POST['pass']) {
        die('You must enter the required data.');
    }
// and so on

The issue is that you're trying to send HTTP headers (Location:) after HTML code.

You should refactor your code to avoid that; or as a hack you can work around it using output buffering, that'll allow you to modify HTTP headers after you started outputting your HTML content. Not a great way to handle the problem IMHO but good to know anyway; see http://ca.php.net/manual/en/function.header.php#refsect1-function.header-notes