2014-11-22 98 views
0

我嘗試登錄加載頁面,但也有兩個誤區:無法發送會話cookie - 標題已經發出(當在session_start())

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at Z:\home\lab4\www\login.php:18) in Z:\home\lab4\www\login.php on line 19 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at Z:\home\lab4\www\login.php:18) in Z:\home\lab4\www\login.php on line 19 

線19 session_start();。我試圖刪除瀏覽器中的所有cookie,但沒有幫助。有任何想法嗎?

<body> 
    <?php 
     session_start(); 
     if(isset($_POST["enter"])) 
     {     
      $login = $_POST["login"]; 
      $password = $_POST["password"]; 
      $query = mysql_query("SELECT * FROM users WHERE password = '$password' AND login = '$login'") or die(mysql_error()); 
      $user_data = mysql_fetch_array($query); 
      if($user_data) 
      {              
       $_SESSION["login"] = $user_data["login"]; 
       $_SESSION["loggedin"] = true; 
      } 
      else print('<script>alert("Incorrect login or password!")</script>'); 
     } 
+0

和空格周圍有落後的session_start() – panther 2014-11-22 12:42:11

+0

也許到這裏看看:http://stackoverflow.com/q/15367131/ 3933332 – Rizier123 2014-11-22 12:42:20

回答

0

session_start()必須在所有輸出之前移動您的PHP代碼。

<?php 

session_start(); 

if (isset($_POST["enter"])) {     
    $login = $_POST["login"]; 
    $password = $_POST["password"]; 
    $query = mysql_query("SELECT * FROM users WHERE password = '$password' AND login = '$login'") or die(mysql_error()); 
    $user_data = mysql_fetch_array($query); 

    if ($user_data) { 
     $_SESSION["login"] = $user_data["login"]; 
     $_SESSION["loggedin"] = true; 
    } 
    else print('<script>alert("Incorrect login or password!")</script>'); 
} 

// another PHP code 

?> 

<body> 
    ... 
+0

沒有幫助。錯誤是一樣的 – Andrew 2014-11-22 12:53:13

+0

你有沒有第19行?在你的問題代碼中,我沒有看到COOKIES。 – panther 2014-11-22 12:54:19

+0

session_start();是第19行 – Andrew 2014-11-22 13:07:48

0

我認爲的代碼將工作

<?php 

session_start(); 

if (isset($_POST["enter"])) {     
    $login = $_POST["login"]; 
    $password = $_POST["password"]; 
    $query = mysql_query("SELECT * FROM users WHERE password = '$password' AND login = '$login'") or die(mysql_error()); 
    $user_data = mysql_fetch_array($query); 

    if ($user_data) { 
     $_SESSION["login"] = $user_data["login"]; 
     $_SESSION["loggedin"] = true; 
     $tpl_body='You are in'; 


    } 
    else $tpl_body='<script>alert("Incorrect login or password!")</script>'; 
} 

// another PHP code 

?> 

<body> 


<?php 

echo $tpl_body; 

?> 

</body> 
相關問題