PHP问题“无法发送会话cookie”

时间:2013-03-12 16:44:25

标签: php session cookies

请附上我的代码:

<?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();
}
}
?>

虽然代码在我的localhost(wampserver)上完美运行,但它在我的主机上无效并且我收到了错误:

  

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

     

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

session_start();代码已放置在PHP块的顶部,在进行任何HTML输出之前,我完全被难倒了。

有什么想法吗?

2 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:-1)

如果从vhindex.php加载此-.php文件。确保以正确的顺序完成此操作。 例如。 require_once&#39;处理用户自的login.php&#39 ;; callToYourFunction();

如果您的会话已经发送,则会收到此警告。 (是的,就像错误代码所说的那样)。