Welcome! Log In Create A New Profile

Advanced

[PHP] ob_start() -- ob_end_flush() problem

Posted by Tedd Sperling 
Tedd Sperling
[PHP] ob_start() -- ob_end_flush() problem
April 27, 2017 05:10PM
Hi guys:

As many of you know, I teach PHP at my local college. I’ve run into a problem and I can’t find a reason/solution.

Here’s the situation — I use ob_start() -- ob_end_flush() (see below) and my code works.

However, when my students try it, even using my code, it doesn’t work for them.

Now, both my students and I are on the same server with supposedly the same settings — what could go wrong? What should we look for?

Many thanks in advance for those providing their time/expertise to help.

Thank you,

tedd

— here’s the code:

<?php /* display-db-image.php

// this script gets an image from an image-field in a db table
// and displays the image "as-is"

/* start buffered output */
ob_start();

$table = 'image_table';

$id = isset($_GET['id']) ? $_GET['id'] : 1;

if(!ctype_digit($id)) // clean -- make sure the $id is a number
{
$id = 1;
}

include('includes/open-db.php'); //====== open dB

$query = "SELECT image FROM $table WHERE id = '$id' ";
$result = mysqli_query($conn, $query) or die("$query Could not get image");

include('includes/close-db.php'); //====== close dB

if(mysqli_num_rows($result) == 1)
{
$row = mysqli_fetch_assoc($result);
$image = $row['image'];

// set the header for the image
header("Content-type: image/jpeg");
echo($image);
}

$page = ob_get_contents();
/* end buffered output */
echo($page);
ob_end_flush();
?>
— end of code

_______________
tedd sperling
tedd@sperling.com






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
David Harkness
Re: [PHP] ob_start() -- ob_end_flush() problem
April 27, 2017 06:00PM
On Thu, Apr 27, 2017 at 8:00 AM, Tedd Sperling <[email protected]> wrote:

> Here’s the situation — I use ob_start() -- ob_end_flush() (see below) and
> my code works.


> However, when my students try it, even using my code, it doesn’t work for
> them.
>

How does it fail? Do they get double output? No output? Fatal error? Car
explodes? You're really not giving us much to go on here. :)

It looks like you're missing some important comments and--though it's early
and I may be a little groggy still--the function of ob_end_flush().

/* start buffered output */
> ob_start();



/* capture buffered output */

$page = ob_get_contents();
>
> /* buffer captured buffered output */
> echo($page);
>
> /* send buffered output and stop buffering */
> ob_end_flush();


Calling ob_get_contents() returns the current buffer *without clearing its
contents*. You then output that right back into the buffer. Finally,
calling ob_end_flush() outputs the current buffer and stops (that level of)
buffering.

My guess is that "it doesn't work" means that the image is corrupted
because it's doubled in the response and you're using a browser that
handles that kind of image corruption.

Cheers!
David
Tedd Sperling
Re: [PHP] ob_start() -- ob_end_flush() problem
April 29, 2017 04:10PM
> On Apr 27, 2017, at 11:57 AM, David Harkness <[email protected]> wrote:
>
> How does it fail? Do they get double output? No output? Fatal error? Car
> explodes? You're really not giving us much to go on here. :)
>

My apologies, you are correct I did not give you much to go on, but then again I don’t have much to go on either.

When I said this fails, I meant it didn’t do what it is supposed to do, namely to capture all the output and echo it at one time.

The unfortunate result is nothing happens at all — it is a blank screen wth no errors, not even in the error log.

If you review the code, you will see the script simply takes an image from a database and echo’s it.

The problem described is that *my code* works for me — whereas, *my code* running on my students accounts does not work.

I’ve been told by the Server’s administrator both my account and my student’s accounts are identical. But this script proves something is different.

So, my question is “What is different?”

To try to figure this out, I am asking this group what could cause this? What could possibly go wrong and what questions should I ask the administrator and help him focus on possible problems? For example, I asked if the settings for the output_buffer were the same and he answered “Yes”. So, I am trying to figure out what else to look for? After all, there *is* something different. We should be able to figure out what.

Again, thanks in advance for any help on this.

Cheers,

tedd
_______________
tedd sperling
tedd@sperling.com






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
Re: [PHP] ob_start() -- ob_end_flush() problem
April 29, 2017 04:30PM
On Sat, Apr 29, 2017 at 10:03 AM, Tedd Sperling <[email protected]> wrote:

>
> > On Apr 27, 2017, at 11:57 AM, David Harkness <[email protected]>
> wrote:
> >
> > How does it fail? Do they get double output? No output? Fatal error? Car
> > explodes? You're really not giving us much to go on here. :)
> >
>
> My apologies, you are correct I did not give you much to go on, but then
> again I don’t have much to go on either.
>
> When I said this fails, I meant it didn’t do what it is supposed to do,
> namely to capture all the output and echo it at one time.
>
> The unfortunate result is nothing happens at all — it is a blank screen
> wth no errors, not even in the error log.
>
> If you review the code, you will see the script simply takes an image from
> a database and echo’s it.
>
> The problem described is that *my code* works for me — whereas, *my code*
> running on my students accounts does not work.
>
> I’ve been told by the Server’s administrator both my account and my
> student’s accounts are identical. But this script proves something is
> different.
>
> So, my question is “What is different?”
>
> To try to figure this out, I am asking this group what could cause this?
> What could possibly go wrong and what questions should I ask the
> administrator and help him focus on possible problems? For example, I asked
> if the settings for the output_buffer were the same and he answered “Yes”.
> So, I am trying to figure out what else to look for? After all, there *is*
> something different. We should be able to figure out what.
>
> Again, thanks in advance for any help on this.
>
> Cheers,
>
> tedd
> _______________
> tedd sperling
> tedd@sperling.com
>
>
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Are you using the same includes/open|close-db.php in both instances? If the
answer is not, can your student try using your db files and see if it works
or vise-versa (you use theirs). The code looks fine and the issue could lie
with the included files/settings.
Arno Kuhl
RE: [PHP] ob_start() -- ob_end_flush() problem
April 29, 2017 07:30PM
> On Apr 27, 2017, at 11:57 AM, David Harkness <[email protected]> wrote:
>
> How does it fail? Do they get double output? No output? Fatal error?
> Car explodes? You're really not giving us much to go on here. :)
>

My apologies, you are correct I did not give you much to go on, but then again I don’t have much to go on either.

When I said this fails, I meant it didn’t do what it is supposed to do, namely to capture all the output and echo it at one time.

The unfortunate result is nothing happens at all — it is a blank screen wth no errors, not even in the error log.

If you review the code, you will see the script simply takes an image from a database and echo’s it.

The problem described is that *my code* works for me — whereas, *my code* running on my students accounts does not work.

I’ve been told by the Server’s administrator both my account and my student’s accounts are identical. But this script proves something is different.

So, my question is “What is different?”

To try to figure this out, I am asking this group what could cause this? What could possibly go wrong and what questions should I ask the administrator and help him focus on possible problems? For example, I asked if the settings for the output_buffer were the same and he answered “Yes”. So, I am trying to figure out what else to look for? After all, there *is* something different. We should be able to figure out what.

Again, thanks in advance for any help on this.

Cheers,

tedd
_______________
tedd sperling
tedd@sperling.com



To try isolate the problem you could test just the output buffering
<?php
/* start buffered output */
ob_start();

// set the header for the image
header("Content-type: text/plain");
echo("test-1-2-3\n");

$page = ob_get_contents();
/* end buffered output */
echo($page);
ob_end_flush();
?>

My output is:
test-1-2-3
test-1-2-3

so as David said the output is repeated (echoed twice before ob_end_flush() ).
If the text output either does or doesn't display on your student's terminal it should highlight where the problem lies.

Cheers
Arno


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tedd Sperling
Re: [PHP] ob_start() -- ob_end_flush() problem
April 29, 2017 10:20PM
> On Apr 29, 2017, at 10:20 AM, Aziz Saleh <[email protected]> wrote:
>
>>
> Are you using the same includes/open|close-db.php in both instances? If the
> answer is not, can your student try using your db files and see if it works
> or vise-versa (you use theirs). The code looks fine and the issue could lie
> with the included files/settings.

Yes, the code in all instances (including the includes) is exactly the same.

I added the --

$page = ob_get_contents();
/* end buffered output */
echo($page);

— in both cases and theirs failed and mine did not.

My original code just used ob_start() and ob_end_flush() and worked just fine. Again, theirs did not.

I’ll try Arno’s suggestion an echo something simple and see if that works.

Thanks,

tedd

_______________
tedd sperling
tedd@sperling.com






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
Re: [PHP] ob_start() -- ob_end_flush() problem
April 29, 2017 10:50PM
On Sat, Apr 29, 2017 at 4:15 PM, Tedd Sperling <[email protected]> wrote:

>
> > On Apr 29, 2017, at 10:20 AM, Aziz Saleh <[email protected]> wrote:
> >
> >>
> > Are you using the same includes/open|close-db.php in both instances? If
> the
> > answer is not, can your student try using your db files and see if it
> works
> > or vise-versa (you use theirs). The code looks fine and the issue could
> lie
> > with the included files/settings.
>
> Yes, the code in all instances (including the includes) is exactly the
> same.
>
> I added the --
>
> $page = ob_get_contents();
> /* end buffered output */
> echo($page);
>
> — in both cases and theirs failed and mine did not.
>
> My original code just used ob_start() and ob_end_flush() and worked just
> fine. Again, theirs did not.
>
> I’ll try Arno’s suggestion an echo something simple and see if that works.
>
> Thanks,
>
> tedd
>
> _______________
> tedd sperling
> tedd@sperling.com
>
>
>
>
>
>
I am all for trusting ops with their word but sometimes you need to confirm
things yourself. Do you see any difference between the 2 servers settings
wise using phpinfo(), or php -i if you are on command prompt.
Serpent7776
Re: [PHP] ob_start() -- ob_end_flush() problem
April 30, 2017 11:40AM
On Sat, 29 Apr 2017 16:38:55 -0400
Aziz Saleh <[email protected]> wrote:

> I am all for trusting ops with their word but sometimes you need to confirm
> things yourself. Do you see any difference between the 2 servers settings
> wise using phpinfo(), or php -i if you are on command prompt.

I remember having some issues with output buffering like this some time ago.
Depending on `output_buffering` setting it was behaving differently on Unix and
Windows systems.
http://php.net/manual/en/outcontrol.configuration.php#ini.output-buffering
You can try to fiddle with this setting and see if it helps.

--
/*
* Serpent7776
*/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tedd Sperling
Re: [PHP] ob_start() -- ob_end_flush() problem
May 02, 2017 04:00PM
> On Apr 29, 2017, at 4:38 PM, Aziz Saleh <[email protected]> wrote:
> I am all for trusting ops with their word but sometimes you need to confirm things yourself. Do you see any difference between the 2 servers settings wise using phpinfo(), or php -i if you are on command prompt.

Good point and consideration.

I actually did run phpinfo() on both accounts and found absolutely no difference.

Thanks,

tedd
_______________
tedd sperling
tedd@sperling.com






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login