0

SO I have read a lot of questions here but none are helping my case.

I have this PHP page which when loaded gives this error :-

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /Library/WebServer/Documents/k/editprofile.php:1) in /Library/WebServer/Documents/k/class/class.user.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/k/editprofile.php:1) in /Library/WebServer/Documents/k/verify_user.php on line 4

I've checked and there is no "echo" or "print" or any whitespaces. What might be the reason for this error?

Files :-

editprofile.php

<?php
include 'class/class.user.php';
include 'common_includes.php';
$user = new user();
.
.
.

class.user.php

<?php
session_start();
include 'dbconnector.php';
class user{
.
.
.

verify_user.php

<?php
if((!isset($_SESSION['logged'])) || ($_SESSION['logged'] != 1))
{
header("Location:login.php?error=Please Login First");
}
?>

dbconnector.php

<?php
$connectionString = 'mysql:host=127.0.0.1;dbname=k';
try
{
$conn = new PDO($connectionString, 'root', 'k');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>

common_includes.php

<?php
include 'verify_user.php';
include 'config.php';
?>
RinSupreme
  • 39
  • 1
  • 1
  • 6

1 Answers1

0

An HTTP response needs to begin with the headers, then the body. In a PHP page, anything you echo or include as mixed HTML is going to go into the body, so if you start echoing, then write the header, it will fail. This is tricky because PHP makes it easy (some would say too easy) to write to the body without knowing it.

From your snippets alone, I see two big things:

  1. You're ending your files with the PHP close ?>. The means everything after it is considered content. Even if it doesn't look like there's anything after it, it's very easy for there to be. An extra space or even a newline will break. The solution: don't bother putting the ?> at the end of a file. There's no advantage to it and most modern PHP style guides actively prohibit it.
  2. You do have echos happening possibly before you run header() in your verify_user.php. If you're going to echo the PDOException, you may want to simply die afterwards, so that it doesn't write the error message, then set the header. Check for any other echoes you may have running before the header.

In general, you want to make sure you're setting the header as early as you possibly can.

Josh from Qaribou
  • 6,776
  • 2
  • 23
  • 21
  • Thank you for the answer. I just made sure that there are no echo's and no trailing spaces. Still doesn't seems to be working. – RinSupreme Jul 13 '17 at 11:14
  • You may also have an uncaught exception. If an exception throws before the header is set, it will be passed to the default exception handler which will write the exception to the stdout, so it falls into the response body. You could try setting your own exception handler than simply logs the error instead of responding with it: http://php.net/manual/en/function.set-exception-handler.php – Josh from Qaribou Jul 13 '17 at 12:10