2010-01-23 117 views
1

我想不通为什么我收到这个会话错误...为什么我得到这个PHP session_start()错误?

警告:在session_start() [function.session启动]:不能发送 会话缓存限制器 - 头 已经发出(输出开始在 C:\网络服务器\ htdocs中\项目2 \实验室\形状的提交\的index.php:2) 在 C:\网络服务器\ htdocs中\项目2 \实验室\形状的提交\的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; 
     } 
    } 
} 
?> 
+0

在解决这个问题上仍然没有运气,它似乎会影响我网站上尝试使用会话的每个文件。很奇怪,现在所有的工作现在都显示出这个错误。 – JasonDavis 2010-01-23 03:25:50

+0

http://stackoverflow.com/questions/1183726/headers-already-sent-in-php的可能重复,http://stackoverflow.com/questions/1891969/php-headers-already-sent-error – outis 2010-05-05 00:28:10

+0

@outis这不是重复的,因为在会话被称为错误之前,这不是简单的空白或OUTPUT。此外,这是近半年的 – JasonDavis 2010-05-09 00:35:58

回答

1

我跑到这个错误一次,当文件在开始时有一个BOM(字节顺序标记)。显然这也会导致标题被发送。但是,这可能是一个自修复以来的PHP错误。值得一看虽然..

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

+0

+1这也是我的回答。 – 2010-01-23 01:02:20

+0

它似乎是我现在整个服务器上的每个文件。我认为这必须是服务器相关的,因为我的服务器上的每个文件都是在今晚开始做这个 – JasonDavis 2010-01-23 03:39:38

+0

实际上,我认为你可能会对某些东西感兴趣,我使用chrome,但是我只是尝试了一些使用firefox和Firefox的页面,我得到了2个错误,一个我在这里显示,但befor该错误是另一个错误“无法发送会话cookie” – JasonDavis 2010-01-23 04:03:46

0

你可能在index.php文件顶部一些空白。该<之前吧?标签,可能是一个空间......这将导致它.. php是非常挑剔的... ... session_start必须在任何输出被发射之前调用...

+0

这就是我最初的想法,但事实并非如此,这是一个非常奇怪的问题 – JasonDavis 2010-01-23 03:06:10