Sammaye
Sammaye

Reputation: 43884

ob_start not working in PHP 5.5

So I have this extremely simple script:

echo "lalalaal";
ob_start();
var_dump(headers_sent());
echo "heretoo";
$html = ob_get_contents();
ob_end_clean();
echo $html;

And it is being run from the command line with:

php n.php

at all times.

I have two servers:

The output on my dev server is:

lalalaal
bool(false)
heretoo

On my live server:

lalalaal
bool(true)
heretoou

I am showing all errors on both servers, why is my live server returning true? What has changed since 5.3 to cause this?

Edit

With var_dumping the results from headers_sent it just tells me that lalalaal causeed it:

lalalaalbool(true)
string(18) "/home/ubuntu/n.php"
int(4)

int(4) pointing to echo "lalalaal"; since it sits under a PHP tag and then a blank line then a comment.

Upvotes: 2

Views: 8091

Answers (4)

JSON
JSON

Reputation: 1835

When using a webserver, PHP will send header information to the browser first thing. PHP's cue to send it's header information is the first time the output buffer is used. Header information is sent the first time something placed into to the output buffer, and there's generally no turning back at that point.

The confusion here is from the fact that you're using the CLI. The CLI didn't use headers related functions prior to PHP 5.4.0 because it was purely a command-line tool - thus it wasn't bound to server specific or browser related behavior. However, the CLI in 5.4.0 and later has a built-in webserver cli-server.

You had different rules regarding headers in CLI mode before 5.4.0 because it wasn't built to handle or acknowledge headers, so headers_sent() would return false no matter what. Thats not the case for 5.4.0 and later, including 5.5.

You will need to follow the normal guidelines regarding headers and the output buffer if you want your CLI script to work as expected in both 5.3 and 5.5.

Upvotes: 9

Abhinav
Abhinav

Reputation: 8168

ob_start() is used to store the contents that you are gonna display in the user's browser.It is used as a BUFFER storage.

EX 1: Take the following Example:

<?php

echo "hi";
header("As you have already displayed "hi", this info will not be sent.);
?>

EX 2: [A practical use of ob_start]

In the below code, the header function will work since the echo informations will be stored in the buffer memory.

<?php
ob_start();
echo "hi";
echo "Hello"
header("This info will be sent");
ob_end_flush();
?>

NOTE: One precaution though you have to take is, you have to include the ob_start() function in the beginning of the script so that it can store the infos in the memory.[This is where you made the mistake in your script by placing it after the echo.]

Upvotes: -1

Darwin
Darwin

Reputation: 87

Try this

<?php
    ob_start(); /* first line, without empty space */
    echo "lalalaal";

    var_dump(headers_sent());
    echo "heretoo";
    $html = ob_get_contents();
    ob_end_clean();
?>

Upvotes: 1

aelgoa
aelgoa

Reputation: 1201

you're sending headers ('e') after you already echoed 'lalalaal' to the client.

Upvotes: 6

Related Questions