Php Session header already send error [duplicate]

2020-02-16 05:03发布

Possible Duplicate:
php headers already sent error

I have attached my code which am using for a very basic login in php. It giving an error

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!";
        }



    }
    ?>

Am not able to find out the problem

6条回答
看我几分像从前
2楼-- · 2020-02-16 05:27

You need to put session_start() before anything outputs on the page.

 <?php
session_start(); 
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_register($data['username']);
        //successful login code will go here...
        //echo 'Logged in!'; 
        }
    }
    else
    {
    echo "Failed!";
    }



}
?>
查看更多
不美不萌又怎样
3楼-- · 2020-02-16 05:27

If you were trying to use any function that modifies the header (like header or session_start), you need to make sure that nothing was outputting beforehand, even whitespace -- or else you will get that error.

查看更多
家丑人穷心不美
4楼-- · 2020-02-16 05:29
include('includes/config.php');

look for white space in that file.

in particular after the ?> at the end of the file there is often white space.

so omit the closing ?> at the end of your files.

查看更多
Explosion°爆炸
5楼-- · 2020-02-16 05:32

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.

查看更多
Juvenile、少年°
6楼-- · 2020-02-16 05:33

Make sure there is absolutely nothing being output before your session call - even whitespace. (See Pekka's comment :) )

查看更多
欢心
7楼-- · 2020-02-16 05:41

Halfway down you're having

session_start();

But you don't want any output before that. And you have -in the very least- this allready sent:

   <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>

PUt the session on the top :D

查看更多
登录 后发表回答