PHP会话标头已发送错误 [英] Php Session header already send error
问题描述
可能重复:
php标头已发送错误
Possible Duplicate:
php headers already sent error
我附上了我的代码,该代码用于在php中进行非常基本的登录.它给出了错误
I have attached my code which am using for a very basic login in php. It giving an error
无法发送会话cookie-标头 已发送者(输出始于 C:\ xampp \ htdocs \ netsentries \ f \ includes \ functions.php:1)
Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\netsentries\f\includes\functions.php:1)
<?php
if (!isset($_POST['username'])) {
?>
<h3>Login</h3>
<form action="" method="post">
Username<br/>
<input name="username" type="text" /><br/>
Password<br/>
<input name="password" type="password" /><br/>
<input name="" type="submit" /><br/>
</form>
<?php
}
else
{
include('includes/config.php');
$data['username']= $_POST['username'];
$data['password']= md5($_POST['password']);
connect($db_name,$db_user,$db_pass,$db_host);
$query= "select * from user where username='" . $data['username'] . "' AND password='" . $data['password'] . "'";
$result=mysql_query($query);
//check that at least one row was returned
$rowCheck = mysql_num_rows($result);
if($rowCheck > 0)
{
while($row = mysql_fetch_array($result))
{
session_start();
session_register($data['username']);
//successful login code will go here...
//echo 'Logged in!';
}
}
else
{
echo "Failed!";
}
}
?>
我无法找出问题所在
推荐答案
在完全输出任何HTML之后,您就不能session_start
.将session_start
调用上移至顶部,并保留一个变量以备后用. <?php ?>
中不包括在内的任何内容,甚至包括您的包含文件.
You can't session_start
after you've outputted any HTML at all. Move the session_start
call up to the top, and keep a variable for what to echo later. Anything not in <?php ?>
counts, even your include file.
这篇关于PHP会话标头已发送错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!