whole page being displayed after process is finished [duplicate] - php

This question already has answers here:
How to disable output buffering in PHP
(6 answers)
Closed 6 years ago.
I have programmed and set up a php web application on a shared Host (using apache server). Before the server being transferred, on each page, header and sidebar and ... was loaded and displayed and each part of the script was displayed immediately after being processed. For example if data was fetched from database and being displayed in a table, after displaying table header, each row was processed and displayed and then the next row and etc.
There has been a server transfer and apparently some configurations might have been changed and now each page is only displayed when the whole page is completely processed and is being displayed all at once.
I was wondering what the problem might be.
Thanks in advance

The issue here is Output Buffering. What happens with Output Buffering is, the output is not sent immediately, but gets sent only after the whole page is processed including delays and sleep.
You can disable output buffering to get the same experience. But beware, you will get the dreadly Headers already sent. errors sometimes.
See How to disable output buffering in PHP to disable output buffering per page or whole.

Related

Is it possible to display the commands like 'top' on webpage using php? [duplicate]

This question already has answers here:
Why doesn't exec("top"); work on Linux?
(5 answers)
Closed 9 years ago.
Is it possible to display the commands like 'top' on webpage using php?
<?php
echo shell_exec('top');
?>
maybe this:
<?php
$output = null;
exec('top -n 1', $output);
var_dump($output);
?>
If I understand your question correctly, you're trying to get an interactive program showing on the client with live updates. This is not possible as you've demonstrated.
It seems you may not understand what's going on with PHP. PHP runs on the server before the page is downloaded by the client. The client then gets a 'snapshot' of the page as the server rendered it. Once the page is loaded on the user's machine, the server cannot touch the page.
To get interactive content, you have a few options (from least desirable and easiest to most desirable and most involved):
refresh the page
make periodic requests for updates (AJAX)
have the server push down changes (COMET, WebSockets)
Another problem is that interactive commands like top use a bunch of terminal-specific (refresh the terminal, rewrite bits of text, etc.) that will mess up the output in the browser. You'll need to do something like #David said and get a snapshot of the output and get that to the user periodically (choose one from above).
There are lots of libraries and tutorials for PHP available for whichever route you choose.

Print out in for loop [duplicate]

This question already has answers here:
how to make echo print out right away in PHP?
(4 answers)
Closed 9 years ago.
Is it possible to print out text in PHP while a for loop is running? I mean normally it runs the whole loop until the end and then you can see all the text. But I need the text every time when the echo is there in the for loop, not after the whole loop. Can someone give me an example?
void flush (void)
Flushes the write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc). This attempts to push current output all the way to the browser with a few caveats. (more)
There are quite a few caveats. Make sure to read the linked page and comment section if you encounter any problems. For example:
Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you may need to send extra whitespace before flushing to get those browsers to display the page.
Keep in mind that Internet Explorer and Safari have a 1k buffer before incremental rendering kicks in, so you'll want to output some padding as well.

Send html during php page loading [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP echo-ing content as page loads
I have a php page that has to load a heavy table, so after the page request there is a lot of time to wait.
Is it possible to send the page initially empty and, during the php page loading, send to the client the results, so one can see what has been loaded so far? (not a loading bar).
Yes. It's called flushing the document early. At its simplest, output some amount of HTML, and then calling flush();. As with most things, the devil is in the details of exactly what and when things would happen.

PHP redirect in include fails [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I am trying to implement a fairly complex 3rd party script that consists of several interconnected scripts. I've included the intro of the script in a simple page of mine. The script basically writes different things to a string based on logic and echoes the string at the end. Sometimes it echoes a form and when you click on submit, the script runs again and based on the new logic echoes different text that displays fine inside my page where it is included in place of the earlier form. (The 3rd party script included in my page also has some includes of its own.)
My problem is that rather than have it echo some of the things it echoes, notably error os success messages, on occasion I would like to have it redirect to another page on my site.
I've done this successfully with other pages of mine. I include a script in a page that writes some header type code. Based on certain parameters or actions by the user that recall the script, the include may redirect to itself or another page. The only thing I have to make sure is that there are no spaces or text written in the course of the include prior to the redirect.
However working with this third party script, although I think I've removed all the white space, it is not letting me redirect. The error message sites the code written in my page that includes the 3rd party script. Here is the msg:
Warning: Cannot modify header information - headers already sent by (output started at /blah blah/stepone.php:5).
Step one is my simple page that calls the 3rd party script.
Am I right that an include can redirect in response to a user action even if there is some text currently displayed? Should I just be checking the third party scripts for white space or is there some structural thing I may be doing wrong.
The 3rd party scripts are too large to put in here otherwise I would put them in.
Thanks.
What you need, probably is ob_start(). This buffers the output and enables you to redirect, even when output is already generated.
Also check out the manual: http://php.net/manual/en/function.ob-start.php
the error you have mentioned happens because of headers being already send to the browser. and when it tries to send the header again it throws an error. so for example.
when you try doing something like this.
echo 'Hello World';
header('Location:some/location.php');
it might throw you an error. hence it is always good to place the redirect header on top. but sometimes we may not want that. in such case you can turn on output buffering by using php's ob_start() according to PHP's definition
This function will turn output buffering on. While output buffering is
active no output is sent from the script (other than headers), instead
the output is stored in an internal buffer
so instead of example code above you can use something like.
ob_start();
echo 'Hello World';
header('Location:some/location.php');
ob_end_flush();
You have to:
use ob_start() at the beginning of your file.
use ob_end_flush() at the end of your file.

What are the pros and cons of output buffering? [duplicate]

This question already has answers here:
PHP output buffering - sounds like a bad idea, is it?
(4 answers)
Closed 9 months ago.
I have read on many websites that using
ob_start();
can enhance your page load times, as it stores the php in a variable and displays it in one go rather than processing the php a bit of a time.
Also it is extremely useful for
header('location: /');
Some people say that this is spaghetti code, but as long as the code is clear and concise to any programmer then this should not be a problem, right?
What are your thoughts to using it, and what do you set as your output buffering, are there pros and cons to how, when and why I should or shouldn't use it.
The main advantage of output buffering is that you can use it with the ob_gzhandler which will compress your output so you use less bandwidth. Good to use if your server is not setup to send php files compressed.
Another advantage is if your script uses a database or other constrained resources and you have some output before closing your connections or releasing those resources. Instead of having this kind of thing:
Connect to database
Start sending output to the user
Wait for the user to receive everything
Close the database connection
You have:
Start buffering
Connect to database
Output some things
Close database connection
Send the buffer to the user.
When your script would need to be connected for 100ms to the database and your user need 300 more to download it, you can understand how output buffering can help releasing some stress on the database connections limit.
I know something coded well using a well configured server could nullify those advantages, but you never know who will code after you and you don't always have control of the server it's running on.
some user dont know php well. so they use ob_start wrongly.
if you are using header functions such as header(), cookie(), session you dont have to send any output. these function have to use from before output.
but some user is to stop sending output using ob_start or output buffering function.
so you could use javascript or meta forwading to forward user.
<script language="javascript"> window.location = 'some.php'; </script>
or you could use meta refresh to forward user.
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=some.php">
if you really need to use header function you must dont send any output(dont forget that enter character or space is or UTF-8 signature is output too)

Categories