警告:session_start():无法发送会话cookie - 已经发送的标头

时间:2017-04-04 05:52:09

标签: php html session cookies

我收到此错误:

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>

这些是我得到的错误:

  

警告:session_start():无法发送会话cookie - 已经在/ home / jomingeo / public_html / tradeschool / core / session中发送的输出(/home/jomingeo/public_html/tradeschool/index.php:2中的输出)第9行的.php

     

警告:session_start():无法发送会话缓存限制器 - 已在/ home / jomingeo / public_html / tradeschool / core / session中发送的报头(在/home/jomingeo/public_html/tradeschool/index.php:2处开始输出)第9行的.php

任何帮助都会受到欢迎,我希望我已经描述了足够的问题,如果没有请问,我可以分享更多信息。 在此先感谢:)

3 个答案:

答案 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';

答案 1 :(得分:1)

对于PHP Sessions的新手来说,这是最令人愤怒的错误之一。

PHP使用浏览器Cookie来管理会话 - Cookie标识单个浏览器会话唯一的会话ID。它首先设置一个cookie,以便在时机成熟时发送给浏览器。

Cookie是放置在HTTP(S)消息的标头部分中的数据。在开始写入正文后,PHP将添加到标题

意外写入正文的一种方法是在HTML中使用空行。这被视为输出,并将阻止PHP更多地写入标题。

检查所有文件,尤其是包含空行的文件。这应该可以解决问题。

答案 2 :(得分:0)

  

检查

如果您在session_start();之前有任何html输出,那么您必须在页面顶部启动对象

在页面顶部添加此行

// Start a object
<?php ob_start(); ?>