2017-04-04 73 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(); ?> 
相关问题