php - 警告 : session_start() [function. session 开始]:无法发送 session cookie - header 已发送

标签 php session

<分区>

Possible Duplicate:
“Warning: Headers already sent” in PHP

当我将它上传到我的服务器时,我的 PHP 登录表单上显示了一个错误。它在我的本地主机上运行良好,但是当我将它上传到服务器时,它给了我这个错误:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/shopping/public_html/biblioteka/index.php:10) in /home/shopping/public_html/biblioteka/index.php on line 45

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/shopping/public_html/biblioteka/index.php:10) in /home/shopping/public_html/biblioteka/index.php on line 45

Warning: Cannot modify header information - headers already sent by (output started at /home/shopping/public_html/biblioteka/index.php:10) in /home/shopping/public_html/biblioteka/index.php on line 49

这是我的 PHP 代码:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="http://localhost/Shopping_biblioteka/css/style.css">
        <title>Title</title>
    </head>
    <body align="center">
        <div id="login">
        <?php
    
        if (isset($_POST['username']) && isset($_POST['password'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            mysql_connect("localhost", "user", "pass") or die(mysql_error());
            mysql_select_db("db") or die(mysql_error());
        mysql_query("SET CHARACTER SET utf8");
        mysql_query("SET NAMES utf8");
            $result = mysql_query("SELECT password,id FROM x9qg6_users
            where username='" . $username . "'");
            if (!$result) {
                echo 'Could not run query: ' . mysql_error();
                exit;
            }
            $row = mysql_fetch_row($result);
            $test = explode(":", $row[0]);
            $user_id=$row[1];
            $userhash = md5($password . $test[1]);
            if ($test[0] === $userhash) {       
                session_start();
                $_SESSION['login_user'] = $user_id;
                $_SESSION['username'] = $username;
                $url = "biblioteka.php";
                header("Location: $url");
            }
        } else {
            echo 'Внесете ги вашите податоци во полињата!';
        }
        // Connects to your Database 
        ?> 
        <form action="" method="POST" accept-charset="UTF-8"> 
            Корисничко име:<br/>
            <input name="username" id="username" type="text"/><br/>
            Лозинка:<br/>
            <input type="password" id="password" name="password"/><br/>
            <input type="submit" value="Логирај се!"/>
        </form>

        </div>
    </body>
</html>

最佳答案

只需在 session_start() 之后添加 ob_start()

<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<html>
    <head>
       ...
       ...

关于php - 警告 : session_start() [function. session 开始]:无法发送 session cookie - header 已发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13742038/

相关文章:

php - 从 Firebase 云消息传递获取 "InvalidTokenFormat, Error 400"

php - 从php脚本在linux中调用hello_word

php - 如何提高查询性能(使用 explain 命令结果 f.e.)

PHP 错误、 bool 逻辑

php - Wordpress:在表中搜索元素

asp.net session 状态模式 "SQLServer"

typescript - 类型 'NextApiRequest' 缺少类型 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' 的以下属性

jquery - 如何调用iframe的unload事件

javascript - 如何使用 ajax 请求 javascript/PHP 在远程服务器上存储 session

java - session 超时未发生