Question

I have a globals.php file included in every file on my site. I'd like to include on this file a CSS file globals.css.

The problem is that if I add the CSS in globals.php and then include it on all file, I get some errors like:

Warning: session_start() [function.session-start]:
Cannot send session cache limiter - headers already sent (output started at /...)
in /... on line 4

or when using

header('Location: ....');

Is there a better-more appropiate solution than using ob_start at the top of globals.php and ob_end_flush at the bottom of the same file, or is this method the right way to operate?

globals.php

<?php
ob_start();

//some costants and functions
?>

<head>
    <link href="/css/globals.css" rel="stylesheet" type="text/css">
</head>

<?php
    ob_end_flush();
?>
Was it helpful?

Solution

You're flushing the buffer at the end of the globals script, turning off the buffering, so if you had something like

header('...'); // this will work, no output yet
include('globals.php'); // flushes buffers, stops buffering, starts output
header('...'); // fails with "headers already send"

if you're doing any header() calls AFTER you include the globals file, then globals should NOT be flushing the buffers.

OTHER TIPS

If you're using sessions, make sure to call session_start before doing anything else. The error message means that you're trying to call session_start after you've already done some output or other things.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top