附上我的代碼:PHP問題「無法發送會話cookie」
<?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();
}
}
?>
雖然代碼工作完全在我的本地(wampserver),但它不是我的主機上工作,我得到的錯誤:
警告:在session_start()[function.session啓動]:無法發送會話cookie的 - 頭已經發出(輸出開始 /.../vhindex.php:1)在/.../第2行的vhindex.php
警告:session_start()[function.session-start]:無法發送會話 緩存限制器 - 已在/.../vhindex.php發送的頭文件(輸出開始於 /.../vhindex.php:1)第2行
session_start();
代碼已放置在PHP塊的頂部,並且在任何HTML輸出完成之前,我完全陷入了困境。
任何想法?
您是否檢查過字節順序標記(BOM)?它發生在你的文件使用UTF8並且你用Windows'Notepad.exe打開它時。 – Geoffroy 2013-03-12 16:47:12
我如何檢查BOM?如果這是造成問題,我該如何擺脫它? – 2013-03-12 16:50:24
使用能夠向您顯示特殊字符的編輯器。 – Geoffroy 2013-03-12 16:51:36