简体   繁体   English

为什么会出现此PHP session_start()错误?

[英]Why am I getting this PHP session_start() error?

I can not figure out why I am getting this session error... 我不知道为什么会出现此会话错误...

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\\webserver\\htdocs\\project2\\labs\\form-submits\\index.php:2) in C:\\webserver\\htdocs\\project2\\labs\\form-submits\\index.php on line 2 警告:session_start()[function.session-start]:无法发送会话缓存限制器-已在C中发送标头(输出从C:\\ webserver \\ htdocs \\ project2 \\ labs \\ form-submits \\ index.php:2开始)。第2行上的\\ webserver \\ htdocs \\ project2 \\ labs \\ form-submits \\ index.php

As far as I knew this happens only when there is some sort of output to the browser before the session_start() function is called, in this case there is nothing printed to screen before the call, not even any white space. 据我所知,只有在调用session_start()函数之前浏览器有某种输出时,这种情况才会发生,在这种情况下,在调用之前没有任何内容显示在屏幕上,甚至没有空格。 Any ideas why I would still get the errors? 为什么我仍然会收到错误的任何想法?

I posted the full source code of this demo so you can see exactly what I used to create the error. 我已发布了此演示的完整源代码,因此您可以确切地看到我用来创建错误的内容。

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

the class file 类文件

<?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;
        }
    }
}
?>

I ran into this error once when the file had a BOM (byte order marker) at the beginning. 当文件开头具有BOM(字节顺序标记)时,我就遇到了此错误。 Apparently that also caused headers to be sent. 显然这也导致发送标头。 However, this may have been a php bug that has been since fixed. 但是,这可能是自修复以来的php错误。 Worth taking a look at though.. 值得一看。

EDIT: At this point, I am thinking that session_start() is throwing an error before it can get the cookie sent. 编辑:在这一点上,我认为session_start()引发一个错误,然后它才能获取发送的cookie。 An early error would get sent to the browser and prevent the cookie from being sent. 早期错误将被发送到浏览器并阻止cookie的发送。 However, in this case, you should see the earlier error on your screen. 但是,在这种情况下,您应该在屏幕上看到之前的错误。 I know this is probably not the issue, but I can't think of what else could be causing the problem. 我知道这可能不是问题所在,但我想不出是什么原因引起的。

you probably have some whitespace at the top of index.php.. right before the <? 您可能在index.php ..顶部的<。?之前有一些空格。 tag, could be a space... that would cause it.. php is very finicky about that... session_start has to be called before any output is emitted... 标记,可能是一个空格...会导致它.. php对此非常挑剔... session_start必须在发出任何输出之前被调用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM