2010-01-23 2 views
1

내가 알아낼 수 없습니다 ...왜 PHP session_start() 오류가 발생합니까? 나는이 세션 오류가있는 이유

경고 :으로 session_start() [function.session-시작] : 보낼 수 없습니다 세션 캐시 리미터 - 헤더 이미 전송 (출력 C에서 시작 : \ \ 웹 서버 \의 htdocs에의 \의 프로젝트 2의 \ 연구소 양식을 제출 \ index.php를 : 2) C 에서 : \ 웹 서버 \ htdocs를 \ 프로젝트 2의 \ 연구소 \ 폼을 제출 \ index.php에 라인이

내가 아는 한 이것은 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> 

당신은 아마 바로 < 전에 .. index.php를 상단에 약간의 공백이

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

+0

가능한 중복 http://stackoverflow.com/questions/1183726/headers-already-sent-in-php, http://stackoverflow.com/questions/1891969/php-headers-already-sent-error – outis

+0

@outis 이것은 세션이 오류라고하기 전에 간단한 공백이나 OUTPUT이 아니기 때문에 중복되지 않습니다. 또한, 이것은 거의 반년입니다. – JasonDavis

답변

1

파일에는 처음에 BOM (바이트 순서 표시 자)이 있습니다. 분명히 그것은 또한 헤더를 보내 게했습니다. 그러나, 이것은 고정 이후 PHP 버그일지도 모릅니다. 그럴만 한 가치가있다 ..

EDIT :이 시점에서, 나는 session_start()가 쿠키를 보내기 전에 오류가 발생한다고 생각하고 있습니다. 조기 오류는 브라우저로 전송되어 쿠키가 전송되는 것을 방지합니다. 그러나이 경우 화면에 이전 오류가 표시되어야합니다. 나는 이것이 아마도 문제가 아니라는 것을 알고 있지만, 그 밖의 무엇이 문제를 일으키는 지 생각할 수는 없다.

+0

+1 내 대답도 될거야. –

+0

이제 내 전체 서버의 모든 파일 인 것 같습니다.내 서버의 모든 파일이 방금이 오늘 밤에 시작한 것처럼 서버가 관련이 있어야한다고 생각합니다. – JasonDavis

+0

글쎄 실제로 내가 당신이 뭔가있을 수 있다고 생각하지만, 크롬을 사용하지만 파이어 폭스와 일부 페이지를 시도하고 파이어 폭스에서 나는 2 개의 오류를 얻습니다. 하나는 내가 여기 보여 주지만 befor 그 오류가 또 다른 오류 "세션 쿠키를 보낼 수 없습니다" – JasonDavis

0

클래스 파일? 태그는 공간이 될 수 있습니다 .. 그게 .. php는 매우 까다롭습니다 ... 어떤 출력이 방출되기 전에 session_start가 호출되어야합니다 ...

+0

그건 내가 처음에 생각한 것인데 그 중 하나가 아니야, 이건 정말 이상한 문제 야. – JasonDavis

관련 문제