【问题标题】:Why am I getting this PHP session_start() error?为什么我会收到此 PHP session_start() 错误?
【发布时间】:2011-01-08 10:52:00
【问题描述】:

我无法弄清楚为什么我会收到此会话错误...

警告:session_start() [function.session-start]:无法发送 会话缓存限制器 - 标头 已经发送(输出开始于 C:\webserver\htdocs\project2\labs\form-submits\index.php:2) 在 C:\webserver\htdocs\project2\labs\form-submits\index.php 在第 2 行

据我所知,只有在调用 session_start() 函数之前有某种输出到浏览器时才会发生这种情况,在这种情况下,在调用之前没有任何内容打印到屏幕上,甚至没有任何空白。任何想法为什么我仍然会收到错误?

我发布了这个演示的完整源代码,这样你就可以准确地看到我用来创建错误的原因。

<?php
session_start();

require('formkey.class.php');
$formKey = new formKey();

$error = 'No error';

//Is request?
if($_SERVER['REQUEST_METHOD'] == 'post')
{
    //Validate the form key
    if(!isset($_POST['form_key']) || !$formKey->validate())
    {
        //Form key is invalid, show an error
        $error = 'Form key error!';
    }
    else
    {
        //Do the rest of your validation here
        $error = 'No form key error!';
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <title>Securing forms with form keys</title>
</head>
<body>
    <div><?php if($error) { echo($error); } ?>
    <form action="" method="post">
    <dl>
        <?php $formKey->outputKey(); ?>

        <dt><label for="username">Username:</label></dt>
        <dd><input type="text" name="username" id="username" /></dd>
        <dt><label for="username">Password:</label></dt>
        <dd><input type="password" name="password" id="password" /></dd>
        <dt></dt>
        <dd><input type="submit" value="Submit" /></dd>
    <dl>
    </form>
</body>
</html>

类文件

<?php
class formKey
{
    //Here we store the generated form key
    private $formKey;

    //Here we store the old form key 
    private $old_formKey;

    //The constructor stores the form key (if one excists) in our class variable
    function __construct()
    {
        //We need the previous key so we store it
        if(isset($_SESSION['form_key']))
        {
            $this->old_formKey = $_SESSION['form_key'];
        }
    }

    //Function to generate the form key
    private function generateKey()
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        $uniqid = uniqid(mt_rand(), true);
        return md5($ip . $uniqid);
    }

    //Function to output the form key
    public function outputKey()
    {
        //Generate the key and store it inside the class
        $this->formKey = $this->generateKey();
        //Store the form key in the session
        $_SESSION['form_key'] = $this->formKey;

        //Output the form key
        echo "<input type='hidden' name='form_key' id='form_key' value='".$this->formKey."' />";
    }


    //Function that validated the form key POST data
    public function validate()
    {
        //We use the old formKey and not the new generated version
        if($_POST['form_key'] == $this->old_formKey)
        {
            //The key is valid, return true.
            return true;
        }
        else
        {
            //The key is invalid, return false.
            return false;
        }
    }
}
?>

【问题讨论】:

  • 修复这个问题仍然没有运气,它似乎会影响我网站上尝试使用会话的每个文件。非常奇怪,以前的所有工作现在都显示此错误。
  • @outis 这不是重复的,因为这不是一个简单的空格或在会话被称为错误之前的输出。还有,这也快半年了
  • 从您的以下问题"PHP Session Cookies stopped working on my server ONLY" 来看,问题是一个 BOM,它是在 session_start 调用之前输出的,因此这是重复的。这个重复的问题中的一些答案也提到了 BOM。至于问题年龄,无关紧要。列出可能的重复项是为了指导人们寻找问题的答案;将问题标记为重复将其链接到其他人提出的相同问题。

标签: php class session


【解决方案1】:

当文件开头有 BOM(字节顺序标记)时,我遇到过这个错误。显然,这也导致发送标头。但是,这可能是一个已修复的 php 错误。不过值得一看..

编辑:此时,我认为 session_start() 在发送 cookie 之前会引发错误。一个早期的错误会被发送到浏览器并阻止 cookie 被发送。但是,在这种情况下,您应该会在屏幕上看到之前的错误。我知道这可能不是问题,但我想不出还有什么可能导致问题。

【讨论】:

  • 现在似乎是我整个服务器上的每个文件。我认为这一定与服务器相关,因为我服务器上的每个文件今晚才开始这样做
  • 其实我认为你可能正在做某事,我使用的是 chrome,但我只是用 firefox 尝试了一些页面,在 firefox 中我得到 2 个错误,我在这里显示的那个错误是另一个错误“无法发送会话 cookie”
  • 我只在 Firefox 中看到了 cookie 错误,但这一定是问题所在,在几个小时前它是如何工作的仍然很奇怪,我现在已经在我的计算机上进行了更改,其他站点的会话也可以工作很好
  • 是的,这很奇怪,它依赖于浏览器。但是,某些浏览器会根据服务器返回的 http 状态码以不同的方式显示信息。无论如何,如果你修复了第一个错误,你应该很高兴。
  • 是的,我仍然卡住了,它只是突然发生的,也许如果我希望重启我的系统
【解决方案2】:

你可能在 index.php.. 的顶部有一些空格,就在

【讨论】:

  • 我一开始也是这么想的,但事实并非如此,这真是一个奇怪的问题
猜你喜欢
  • 1970-01-01
  • 2018-06-22
  • 2016-01-01
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多