-1

I am getting this error:

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>

These are the errors I get:

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

Any help would be welcome and I hope I have described enough the problem, if not please ask and I can share more information. Thanks in advance :)

Community
  • 1
  • 1
dark_illusion_909099
  • 1,067
  • 2
  • 21
  • 41

3 Answers3

3

The problem is exactly as it says. You've just output <!-- Session Section --> to the browser so you can't set anymore headers. Do this instead, which uses PHP comments rather than HTML comments.

<?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';
Jonathan
  • 2,778
  • 13
  • 23
1

This is one of the most infuriating errors for newcomers to PHP Sessions.

PHP uses Browser Cookies to manage sessions — the cookie identifies the session id unique to an individual browser session. It does this first by setting a cookie to be sent to the browser when the time comes.

Cookies are data placed in the header section of the HTTP(S) message. PHP will not add to the header after it has started writing to the body.

One way to accidentally write to the body is to have empty lines in the HTML. This is regarded as output, and will prevent PHP from writing more to the header.

Check all of your files, especially the included files for empty lines. That should solve the problem.

Manngo
  • 14,066
  • 10
  • 88
  • 110
0

Check this

if you have any html output before session_start(); so you have to start object in top of page

Add this line on top of page

// Start a object
<?php ob_start(); ?>
ImBhavin95
  • 1,494
  • 2
  • 16
  • 29