Php 启动会话时已发送头

Php 启动会话时已发送头,php,session,Php,Session,运行我的php代码时,会出现以下错误: Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/chatvik/public_html/control.php:2) in /home/chatvik/public_html/control.php on line 5 Warnin

运行我的php代码时,会出现以下错误:

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/chatvik/public_html/control.php:2) in /home/chatvik/public_html/control.php on line 5

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/chatvik/public_html/control.php:2) in /home/chatvik/public_html/control.php on line 5
出了什么问题以及如何解决?以下是代码,出于以下原因,我已删除了真实的db用户名和密码,感谢您的帮助:

<?php

/*** begin our session ***/
session_start();

/*** check if the users is already logged in ***/
if(isset( $_SESSION['user_id'] ))
{
$message = 'Users is already logged in';
}
/*** check that both the username, password have been submitted ***/
if(!isset( $_POST['username'], $_POST['password']))
{
$message = 'Please enter a valid username and password';
}
/*** check the username is the correct length ***/
elseif (strlen( $_POST['username']) > 20 || strlen($_POST['username']) < 4)
{
$message = 'Incorrect Length for Username';
}
/*** check the password is the correct length ***/
elseif (strlen( $_POST['password']) > 6 || strlen($_POST['password']) < 4)
{
$message = 'Incorrect Length for Password';
}
/*** check the username has only alpha numeric characters ***/
elseif (ctype_alnum($_POST['username']) != true)
{
/*** if there is no match ***/
$message = "Username must be alpha numeric";
}
/*** check the password has only alpha numeric characters ***/
elseif (ctype_alnum($_POST['password']) != true)
{
    /*** if there is no match ***/
    $message = "Password must be alpha numeric";
}
else
{
/*** if we are here the data is valid and we can insert it into database ***/
$phpro_username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$phpro_password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);

/*** connect to database ***/
/*** mysql hostname ***/
$mysql_hostname = 'localhost';

/*** mysql username ***/
$mysql_username = 'xxxxxxx_usr';

/*** mysql password ***/
$mysql_password = 'xxxxxxx';

/*** database name ***/
$mysql_dbname = 'xxxxxxx_usr';

try
{
    $dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
    /*** $message = a message saying we have connected ***/

    /*** set the error mode to excptions ***/
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    /*** prepare the select statement ***/
    $stmt = $dbh->prepare("SELECT phpro_user_id, phpro_username, phpro_password FROM phpro_users 
                WHERE phpro_username = :phpro_username AND phpro_password = :phpro_password");

    /*** bind the parameters ***/
    $stmt->bindParam(':phpro_username', $phpro_username, PDO::PARAM_STR);
    $stmt->bindParam(':phpro_password', $phpro_password, PDO::PARAM_STR, 40);

    /*** execute the prepared statement ***/
    $stmt->execute();

    /*** check for a result ***/
    $user_id = $stmt->fetchColumn();

    /*** if we have no result then fail boat ***/
    if($user_id == false)
    {
            $message = 'Login Failed';
    }
    /*** if we do have a result, all is well ***/
    else
    {
            /*** set the session user_id variable ***/
            $_SESSION['user_id'] = $phpro_username;

            /*** tell the user we are logged in ***/
            header('Location:sucsess.htm');
    }


}
catch(Exception $e)
{
    /*** if we are here, something has gone wrong with the database ***/
    $message = 'We are unable to process your request. Please try again later"';
}
}
?>

很抱歉问这个问题。我直接不擅长php,所以我很高兴你能帮助我
感谢您的帮助

尝试添加<代码>开头和结尾您是否尝试过删除空格(2和5处的CRLF)以查看是否发送了该空格?

确保在
之前有空格这不是一个解决方案,而是一个解决办法,在这方面不是很好…更好,但不会将我重定向到正确的位置工作,但之后我不会被重定向到正确的位置…我已经用可能的解决方案更新了我的答案拼写错误应该在那里
header('Location:sucsess.htm');
// shouldn't this be?
header('Location: success.htm');
       add space ^   ^ typo fix