2

请在附件中找到我的代码:

<?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-start]:无法发送会话 cookie - 标头已由第 2 行 /.../vhindex.php 中的(输出开始于 /.../vhindex.php:1)发送

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

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

有任何想法吗?

4

2 回答 2

8
  • 下载Notepad++并打开文件,删除前面的所有花哨的字符<?PHP
  • 确保在<?PHP.
    • 在 Notepad++ 中单击Encoding,然后UTF-8 without BOM将文件转换为不带 BOM 的 UTF-8,然后保存。
  • 为了安全起见,还要添加ob_start();之前。session_start();
于 2013-03-12T16:55:16.887 回答
-1

如果你从 vhindex.php 加载这个 -.php 文件。确保以正确的顺序完成此操作。例如。require_once'handle-user-login.php'; callToYourFunction();

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

于 2015-02-10T13:36:08.513 回答