附上我的代码: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