PHP问题"无法发送会话cookie"

Kay*_*man 2 php cookies session

请附上我的代码:

<?PHP 
session_start();
if (isset($_POST["usernameform"])){ 

$username = $_POST['usernameform'];
$password1 = $_POST['passwordform'];


$user_name = "XXXX";
$password = "XXXXX";
$database = "XXX";
$server = "XXXX";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);


$SQL = "SELECT * FROM login WHERE Username = '$username' AND Password = '$password1' "; //grab all the records from table
$result = mysql_query($SQL)
    or die("Error:" . mysql_error());

if (mysql_num_rows($result) > 0){ //if username and password match return number of rows is always 1

$_SESSION['login'] = $username; //by placing this in session it will remember this variable on the page it directs too

while ( $row = mysql_fetch_assoc($result) ){ //lays out array in $result
$_SESSION['ID'] = $row['ID']; //selects from list of array ID
$_SESSION['firstname'] = $row['First_Name'];
echo'<script> window.location="page1.php"; </script> ';

}
} else {
$_SESSION['login'] = '';
print('
<script type="text/javascript"> //place html script for alert. Use single comma for print command here.
    alert("Sorry, your username or password could not be recognized")
</script>
');
session_destroy();
}
}
?>
Run Code Online (Sandbox Code Playgroud)

虽然代码在我的localhost(wampserver)上完美运行,但它在我的主机上不起作用,我得到错误:

警告:session_start()[function.session-start]:无法发送会话cookie - 已在第2行的/.../vhindex.php中发送(由/.../vhindex.php:1开始的输出)的标头

警告:session_start()[function.session-start]:无法发送会话缓存限制器 - 已在第2行的/.../vhindex.php中发送的标头(在/.../vhindex.php:1处开始输出)

session_start();代码已被放置在PHP代码块的顶部和任何HTML输出是由和我完全难倒了.

有任何想法吗?

pow*_*tac 9

  • 下载Notepad ++并在那里打开文件,删除之前的所有花哨字符<?PHP
  • 确保在之前没有像""或制表符或换行符这样的空白字符<?PHP.
    • 在Notepad ++中单击Encoding,然后UTF-8 without BOM将文件转换为不带BOM的UTF-8,然后保存.
  • 另外添加ob_start();之前session_start();要安全.