2017-04-04 105 views
-1

我收到此錯誤發送了頭:警告:在session_start():不能發送會話cookie - 已經由

Warning: session_start(): Cannot send session cookie - headers already sent by 

的index.php

<!-- Session Section --> 
<?php include 'core/session.php'; ?> 

<!-- Header Section --> 
<?php include 'include/header.php'; ?> 

<!-- Navigation Section --> 
<?php include 'include/navigation_without_logged.php'; ?> 

<!-- Content Section --> 
<?php include 'include/index_content.php'; ?> 

<!-- Footer Section --> 
<?php include 'include/footer.php'; ?> 

session.php文件

<?php 
if(session_status()!=PHP_SESSION_ACTIVE) { 
    session_start(); 
} 
error_reporting(0); 

//finding the page the user is currently on 
$current_page = end(explode('/', $_SERVER['SCRIPT_NAME'])); 

?> 

header.php

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>TEST</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <meta charset="utf-8"> 
    <meta name="keywords" content="Electrician Responsive web template, Bootstrap Web Templates, Flat Web Templates, Android Compatible web template, 
Smartphone Compatible web template, free webdesigns for Nokia, Samsung, LG, SonyEricsson, Motorola web design" /> 
    <script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script> 
    <!-- bootstrap-css --> 
    <link href="assets/css/bootstrap.css" rel="stylesheet" type="text/css" media="all" /> 
    <!--// bootstrap-css --> 
    <!-- css --> 
    <link rel="stylesheet" href="assets/css/style.css" type="text/css" media="all" /> 
    <link rel="stylesheet" href="assets/css/flexslider.css" type="text/css" media="screen" property="" /> 
    <!--// css --> 
    <!-- font-awesome icons --> 
    <link rel="stylesheet" href="assets/css/font-awesome.min.css" /> 
    <!-- //font-awesome icons --> 
    <!-- font --> 
    <link href="//fonts.googleapis.com/css?family=Josefin+Sans:100,100i,300,300i,400,400i,600,600i,700,700i" rel="stylesheet"> 
    <link href='//fonts.googleapis.com/css?family=Roboto+Condensed:400,700italic,700,400italic,300italic,300' rel='stylesheet' type='text/css'> 
    <!-- //font --> 
    <script type="text/javascript" src="assets/js/jquery-2.1.4.min.js"></script> 
    <script src="assets/js/main.js"></script> 

    <!--[if lt IE 9]> 
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> 
    <![endif]--> 

</head> 

這是我得到的錯誤:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/jomingeo/public_html/tradeschool/index.php:2) in /home/jomingeo/public_html/tradeschool/core/session.php on line 9

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/jomingeo/public_html/tradeschool/index.php:2) in /home/jomingeo/public_html/tradeschool/core/session.php on line 9

任何幫助將受到歡迎,我希望我所描述的不足的問題,如果不是請你,我可以分享更多的信息。 在此先感謝:)

+2

的可能的複製[如何修復「頭已發送」錯誤在PHP中](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) –

+0

我已經看過其他地方之前,我張貼但我似乎無法解決問題 –

回答

3

問題與其說的一樣。您剛剛向瀏覽器輸出<!-- Session Section -->,因此您無法再設置標題。這樣做,它使用PHP註釋而不是HTML註釋。

<?php 
// Session Section 
include 'core/session.php'; 

// Header Section 
include 'include/header.php'; 

// Navigation Section 
include 'include/navigation_without_logged.php'; 

// Content Section 
include 'include/index_content.php'; 

// Footer Section 
include 'include/footer.php'; 
+0

是的,它是使用引起問題的評論 –

1

這是PHP Sessions新手最令人生氣的錯誤之一。

PHP使用瀏覽器Cookie來管理會話 - 該cookie標識了單個瀏覽器會話唯一的會話ID。它首先通過設置一個cookie在需要時發送給瀏覽器。

Cookie是放置在HTTP(S)消息標題部分的數據。 PHP將不是添加到標頭它已經開始寫入身體。

一種方式不小心寫入正文是在HTML中有空行。這被認爲是輸出,並會阻止PHP將更多內容寫入標題。

檢查您的所有文件,特別是包含空行的文件。這應該可以解決問題。

0

Check this

,如果您有任何HTML輸出之前session_start();所以你必須在頁面的頂部開始對象

添加此行頁面頂部

// Start a object 
<?php ob_start(); ?> 
相關問題