PHP无法突然使用会话或标头

时间:2014-03-28 21:04:35

标签: php html

我有一个登录我的网站,直到今天它一直工作正常。我试图登录创建一个帖子,但它没有工作,并将其输出到error_log文件:

[28-Mar-2014 14:54:10] PHP Warning:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent by (output started at /home8/encantoj/public_html/discfiresoftworks/login.php:3) in /home8/encantoj/public_html/discfiresoftworks/login.php on line 8
[28-Mar-2014 14:54:10] PHP Warning:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home8/encantoj/public_html/discfiresoftworks/login.php:3) in /home8/encantoj/public_html/discfiresoftworks/login.php on line 8
[28-Mar-2014 14:54:10] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home8/encantoj/public_html/discfiresoftworks/login.php:3) in /home8/encantoj/public_html/discfiresoftworks/login.php on line 30

login.php中的代码如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<?php

include 'includes/conn.php';
include 'includes/hash.php';

session_start();

$msg = "";

if(isset($_POST['username']) && isset($_POST['password'])){

    $user = $_POST['username'];
    $password = $_POST['password'];

    $query = $conn->prepare("SELECT Password, Verified, Admin FROM Users WHERE Name=?");
    $query->bind_param('s', $user);
    $query->execute();
    $query->bind_result($hash, $ver, $admin);

    if($query){

            while($query->fetch()){

                if($ver == "T"){
                        if(validate_password($password, $hash)){
                            $_SESSION['user'] = $user;
                            $_SESSION['admin'] = $admin;
                            header("Location: /");
                        }else{
                            $msg = 'Incorrect username or password';
                        }
                }else{
                    $msg = 'Your account has not been verified, please check your email and do so now if you wish to continue.';
                }

            }

    }else{
        $msg = 'An error occurred when trying to log you in ( ' . $query->errno .' ): ' . $query->error;
    }

}

?>

<html>

    <head>

        <title>DiscFire Softworks - Login</title>

        <!--[if lte IE 10]>
            <link rel="stylesheet" type="text/css" href="includes/ie-styles.css" />
        <![endif]-->

        <!--[if IE 11]>
            <link rel="stylesheet" type="text/css" href="includes/styles.css" />
        <![endif]-->

        <style type="text/css">

            @import url('includes/styles.css');

        </style>

    </head>

    <body>

        <div class="body">

            <img src="images/header.jpg" />

            <div class="navbar">

                <?php

                    $query = $conn->prepare("SELECT Name, Parent FROM pages ORDER BY ID asc");
                    $query->execute();
                    $query->bind_result($name, $parent);

                    while($query->fetch())
                    {
                        if($parent == "self")
                        {
                            echo '<form action="/"><input name="page" type="submit" value="' . $name . '" /></form>';
                        }
                    }

                ?>

            </div>

            <p style="margin-left: 90px;"><?php echo $msg; ?></p>

            <form action="" method="POST">
                <label for="username">Username: </label>
                <input type="text" name="username" style="width: 40%;" value="<?php echo $user; ?>"/>
                <br />
                <label for="password">Password: </label>
                <input type="password" name="password" style="width: 40%; margin-left: 2px;" value="<?php echo $password; ?>"/>
                <input type="submit" value="Login" />
            </form>

        </div>

    </body>

</html>

编辑:另一个问题,我的doctype仅在PHP代码之前有效,但是在输出后我无法使用标题...任何解决方案?

1 个答案:

答案 0 :(得分:3)

<?php session_start();?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<?php

include 'includes/conn.php';
include 'includes/hash.php';

$msg = "";

if(isset($_POST['username']) && isset($_POST['password'])){

    $user = $_POST['username'];
    $password = $_POST['password'];

    $query = $conn->prepare("SELECT Password, Verified, Admin FROM Users WHERE Name=?");
    $query->bind_param('s', $user);
    $query->execute();
    $query->bind_result($hash, $ver, $admin);

    if($query){

            while($query->fetch()){

                if($ver == "T"){
                        if(validate_password($password, $hash)){
                            $_SESSION['user'] = $user;
                            $_SESSION['admin'] = $admin;
                            header("Location: /");
                        }else{
                            $msg = 'Incorrect username or password';
                        }
                }else{
                    $msg = 'Your account has not been verified, please check your email and do so now if you wish to continue.';
                }

            }

    }else{
        $msg = 'An error occurred when trying to log you in ( ' . $query->errno .' ): ' . $query->error;
    }

}

?>

<html>

    <head>

        <title>DiscFire Softworks - Login</title>

        <!--[if lte IE 10]>
            <link rel="stylesheet" type="text/css" href="includes/ie-styles.css" />
        <![endif]-->

        <!--[if IE 11]>
            <link rel="stylesheet" type="text/css" href="includes/styles.css" />
        <![endif]-->

        <style type="text/css">

            @import url('includes/styles.css');

        </style>

    </head>

    <body>

        <div class="body">

            <img src="images/header.jpg" />

            <div class="navbar">

                <?php

                    $query = $conn->prepare("SELECT Name, Parent FROM pages ORDER BY ID asc");
                    $query->execute();
                    $query->bind_result($name, $parent);

                    while($query->fetch())
                    {
                        if($parent == "self")
                        {
                            echo '<form action="/"><input name="page" type="submit" value="' . $name . '" /></form>';
                        }
                    }

                ?>

            </div>

            <p style="margin-left: 90px;"><?php echo $msg; ?></p>

            <form action="" method="POST">
                <label for="username">Username: </label>
                <input type="text" name="username" style="width: 40%;" value="<?php echo $user; ?>"/>
                <br />
                <label for="password">Password: </label>
                <input type="password" name="password" style="width: 40%; margin-left: 2px;" value="<?php echo $password; ?>"/>
                <input type="submit" value="Login" />
            </form>

        </div>

    </body>

</html>

session_start()放在脚本顶部