-1

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();
?>
Perocat
  • 1,481
  • 7
  • 25
  • 48

2 Answers2

2

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Yes, I am doing that... So how can I include a css file in globals.php? – Perocat Jul 29 '13 at 15:27
  • 2
    What you're doing is fine. just **REMOVE** the ob_end_flush() stuff in globals. That's where the problem kicks in. – Marc B Jul 29 '13 at 15:28
  • Ok, but if I call ob_start() after globals.php without calling ob_end_flush at the end of globals.php, will it fails? – Perocat Jul 29 '13 at 15:29
  • 2
    you can call ob_start() as many times as you want. each will just open a new buffer, and exiting the script will automatically flush/send any buffers still open. technically you never HAVE to call end_flush() yourself. – Marc B Jul 29 '13 at 15:31
  • Thank you for the clarification! (I do not understand why my question received '-1'. To me, it appears as a properly question! Maybe I don't know this site well enought) – Perocat Jul 29 '13 at 15:32
  • I still have a problem: on the Javascript alert box I get always `', because of the AJAX reply. How can I prevent that? Thank you – Perocat Jul 29 '13 at 16:13
  • because your code structure is wrong, then. instead of putting css headers into a 'globals' file, it should be in whatever file outputs your site's boilerplate `` section. – Marc B Jul 29 '13 at 16:20
  • So the only solution is to include the `global.css` file in every document's ``? This is what I wanted to avoid... But if it's the only solution.... – Perocat Jul 29 '13 at 16:22
  • no, but don't output the css header unless it's necessary. e.g. if you're doing ajax responses, you don't need to be dumping out an `...` container... – Marc B Jul 29 '13 at 16:31
0

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.

Ambroos
  • 3,456
  • 20
  • 27