I am using PHPSavant templating system for a project and I am not sure how to use ob_start in this.
I have tried before .. for example,
page_header.php
-- ob_start();
page_footer.php
-- ob_end_flush();
But because now I am using a templating system.. am not sure where to put these function.
$template = new Savant3();
$template->some_var = $some_value;
$template->display('default_template');
the default_template contains all of and populate section using some variables (set to $template object). Should I be using ob_start and ob_end_flush where my html code is or to include on each and every php file which calls to this template?
Any ideas? thanks.
You don't have to force a flush, when the PHP script terminates the buffer is flushed.
As long as you put ob_start() at the beginning of your script, that's the best place. In fact you might want to force GZIP compression which will greatly speed up your page display. It seems most servers have GZIP disabled, but you can force it on in your PHP via:
ob_start('ob_gzhandler');
I guess that display method actually outputs the template, so that's the one you should wrap with ob_start and ob_end_flush. However I don't really see advantage of using ob_end_flush around single function call.
Related
I have a php 5.4/mysql website with 5 million hits per day, running on a linux server with nginx and php-fpm. Database is located on a separate server.
I've noticed, that at peak times, my webserver load gets up to 15, instead of normal 4 for quad core processor. I've profiled my php application with xdebug and xhprof, and saw, that 90% of CPU work is done by htmlspecialchars() function in Twig templates that I use to display data. There are sometimes from 100 to 1000 htmlspecialchars() calls per page. I've tried to reduce unnacessary escaping, but still it cannot be avoided.
Is there any way I can reduce CPU usage by htmlspecialchars() function? Maybe there is some kind of caching in php for this? Or there is there another way?
Don't use Twig. Just use php-files with this code:
<?php
// Load a php-file and use it as a template
function template($tpl_file, $vars=array()) {
$dir='/usr/local/app/view/'.$tpl_file.'.php';
if(file_exists($dir)){
// Make variables from the array easily accessible in the view
extract($vars);
// Start collecting output in a buffer
ob_start();
require($dir);
// Get the contents of the buffer
$applied_template = ob_get_contents();
// Flush the buffer
ob_end_clean();
return $applied_template;
}
}
I know that ob_start turns on output buffering, but I don't fully understand what it means. To me it means that it just stops outputting the script data.
Is this true? How does the browser output data in this case, do I have to use ob_end_flush() to turn it off in the end?
Since ob_gzhandler compresses web pages, how do browsers handle these pages?
I have seen ob_start("gzhandler") in code, since ob_gzhandler compresses web pages, what does ob_start("gzhandler") mean and how does it apply to both functions?
All help appreciated!
Output buffering means that instead of writing your output directly to the stdout stream, it is instead written to a buffer.
Then when the script finishes (or when you call ob_end_flush()), the contents of that buffer are written to stdout.
Using ob_gzhandler transforms the contents of the buffer before writing it to stdout, such that it is gzip compressed. (Browsers which support gzip compression reverse this on the opposite end, decompressing the content.)
Ok, let me explain it like this,
It is only one of the uses of the buffer system but I think it's kinda cool.
first I want you to look to this animation.
Operating System Start
When you have a php script that has a level based structure like this, for example you may write:
Connection established to database server..
Database selected : my_database
Data query started
Data query ended (found:200 rows)
...
etc. but if you don't use output buffering and flushing, you will see these lines when all of your script execution ends. But, when the thought is "I want to see what my script is doing when!", you first need to..
Sorry you first need to set implicit_flush to "on" at your php.ini file and restart your apache server to see all of this.
second, you need to open the output buffering (shorthand "ob") by "ob_start();", and then,
place anywhere on your code "echo" statements and after that "ob_flush();" commands to see your script running on realtime.
Later, it is also used for file based static content buffering like this:
place ob_start() at the start of your page (or the start of content you want to capture)
place ob_end_flush() at the end of your page (or the end of content you want to capture);
then $my_var = ob_get_contents(); to get all the HTML output that server creates and sends to the client into my_var variable and then use it as you want. Mostly it's saved to a file and by checking the file's last modification date, it's used as a static buffering.
I hope I could light some bulbs on your mind.
for the download forced, when I save the file it has some extra html test.
my code
<?php
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="abc.txt"');
echo "test";
?>
when I save this is what I have
<script language="javascript">
// some garbage
//-->
</script>
test
I want only test.
Don't include the include file.
At the risk of stating the obvious, something is already outputting some code/including a file, etc. before you're trying to set the headers.
Whilst the cause of this is likely to be specific to your web app (you'll need to check precisely what's being output from the ground up), the requirement is that the headers need to be the very first (before any HTML, etc.) things output.
Based on your comment, do you mean that the JavaScript being outputted is in the PHP file that's doing the outputting, and you want it to not output that?
The first thought would be to remove the JavaScript. But beyond that, you may find some use from something like ob_start() to capture your output buffer and manipulate it before sending it to the client.
I am having a php file which executes some code to generate a html file.Its like I m having a form from which some data will be posted to x.php file, which gives a output(a webpage). I want to save that output in a html file.What the most efficient way of doing this.?
EDIT
I want to save it on sever side. Actually the thing is i want to create pdf file for that.. I wrote everything else.Now the thing is i want to save the output in a html page.So that i can convert it into pdf file..
Try something like this:
// Start output buffering
ob_start();
// run code in x.php file
// ...
// saving captured output to file
file_put_contents('filename.htm', ob_get_contents());
// end buffering and displaying page
ob_end_flush();
If you cannot use the ob_* functions, you can also write the form to a variable and then save that variable.
Look at ob functions (see http://php.net/manual/en/function.ob-start.php) that allows you to capture every output (echo, print, etc...) from the page.
using ob_* functions such as ob_get_contents(), a php script can catch it's output.
probably with ob_start and output_callback see http://php.net/manual/en/function.ob-start.php
I have a php that makes some maintenance operations in my web and I need that the last operation it'll do is to save into a file the content of the screen. I mean, self content.
If the screen shows: "OP1 - OK ..." it has to save into a file this: "OP1 - OK ...".
Saving all the results of my operations into a variable will very hard for me. That's why I need to get the content of the self screen.
Is there any way to do this?
I think you can use an output buffer.
The following is from ob_start's php manual page:
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.
The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents.