Can PHP's ob_start be called more then once?
Sorry if this is a dumb question but I really don't know.
My site is really large (file quantity), its a social network and one of the included files uses ob_start PHP's output buffer for something, i'm not ure someone else started my site a long time ago and now it is mine I need to look into it more to see what it's doing exactly.
Anyways I am wanting to use ob_start ("ob_gzhandler"); to compress CSS files and all files on my site get loaded (included) through the index file so I am wanting to know if I am able to use that even though it is already in use somewhere else in the code?
Yes, you can call it more than once. It creates a new buffer each time however, so be careful.
From the manual: "Output buffers are stackable, that is, you may call ob_start() while another ob_start() is active. Just make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order."
You say this :
I am wanting to use ob_start
("ob_gzhandler"); to compress CSS
files
I would rather serving and compressing JS/CSS (well, static) files is the job of the Web server (ie, Apache), and not PHP.
About that, you can take a look at mod_deflate -- at least, if you are using Apache 2.
all files on my site get loaded
(included) through the index file
Is that really necessary ? You're having PHP work with no apparent (?) reason, that way.
(Note that even if CSS/JS files are served through PHP, Apache should be able to compress them with mod_deflate ; same is also true for HTML, JSON, ... btw)
Another advantage of not going through PHP to server those files is that it would be easier to get them served by another server, as your site will grow (if it grows enough, actually) :
you could have a bunch of "PHP servers", to process PHP pages
and one or two "static-files servers", to server only CSS/JS/images, and lighten the load or your "application servers" ; no need for PHP on these ones ; you could also use something like lighttpd instead of Apache
That being said, ob_start says this :
Output buffers are stackable, that is,
you may call ob_start() while another
ob_start() is active. Just make sure
that you call ob_end_flush() the
appropriate number of times. If
multiple output callback functions are
active, output is being filtered
sequentially through each of them in
nesting order.
So, I think the answer to your question is "yes" :-)
Related
<?php
include 'components/server.php';
Is it possible to make it include server.php for desktops and server-mobile.php for mobile devices?
While technically possible, it's absolutely not the best way of doing things.
Why?
Because PHP runs on the server and only the output of that PHP execution is given to the browser. You would probably be wanting something using javascript which can load and then seamlessly react to the browser conditions, such as screen size and/or dimensions.
If you're trying to change which PHP script is running based on the browser criteria (as mentioned above) this sounds very much like your programming logistics are simply wrong.
If you somehow really do need to change PHP script execution based on end-client (browser) characteristics you could do this by calling a script based on javascript AJAX or using mechanisms mentioned in comments above, but as said, you're almost certainly "doing it wrong".
Alternative
It would be far better to load everything you need in PHP and then pass all of that content to the browser (as output; HTML, CSS, Javascript, etc.) for the Javascript in the browser to then decide which parts of the data it needs to use and ignoring the others.
This question has to do with the internal mechanics of the PHP engine. I'm asking it in an effort to understand the file include process.
Say, you got a 20,000 lines include file ( something like "functions_library.php" ) that gets included on all your php scripts.
Does PHP check/verify if that include file is syntactically correct every single time one of your php scripts load that file? Does this process happen at each page load over and over and over again?
Or...
Does PHP pay attention to the file's last modification date? If it turns out that there were no changes to it since the last check, does it simply ignore the checking?
On a default installation, the file will be parsed every single time. However, any production installation of PHP is recommended to have a bytecode cache, such as APC or many others. When bytecode cache is used, the script is parsed the first time and the interpreted code will be stored in memory.
Different configurations may alter how often file modifications are checked. Under some configurations, for very high performance, manual flushing or restarting the web server may be required.
If you include that file, PHP will need to interpret it every time.
PHP will re-interpret the file every time you call include().
This can be tested visually with the use of variables. If you have something like this:
otherScript.php:
<?php echo $foo; ?>
script.php:
<?php
$foo = 1;
include("otherScript.php");
$foo = 2;
include("otherScript.php");
?>
The output will be:
12
This is a poor example, but it should demonstrate the PHP will need to include the contents of the file each time. It is a pretty good guess to state that the PHP interpreter won't keep a copy of the included file in memory for each reference - there can be hundreds of includes per application.
To deal with this specific ideal, however, they provide a method include_once() that will, as the name implies, only include the file one time and prevent it from being included any additional times.
Is it possible to have print_r() displayed live. By live I mean while the script is executed. I do not want to wait the end of the script to have it displayed. Hope I am clear. Thank you in advance for your replies. Cheers. Marc
Likely you are using PHP through a webserver like Apache.
Webservers have caching implemented, they tend to send their data out in larger blocks.
Browsers also have caching implemented, they only refresh the data from time to time and at the end when they finished loading the website.
Finally PHP also has caching built in.
HTTP was not made for "live" display it's more like a static page, that's why people invented "AJAX" and Javascript to poll for changed/live events after a page was loaded.
What you can do:
To make sure the data from PHP is sent to the webserver you can use the command flush()
There is also a php setting called implicit_flush you might want to look up.
The webserver is likely using gzip/mod_gzip to compress output. You need to disable that behaviour.
Maybe that will do it: #apache_setenv('no-gzip', 1);
Add some more content than just pure text, if you put the data inside a simple "table" including </table> it's more likely browsers will display it during load.
Look in php ini for this:
output_buffering = Off
zlib.output_compression = Off
You can do this at runtime too (#ini_set('zlib.output_compression', 0);)
Some browsers will only display data if they receive a certain amount of bytes.
If I recall right 256 byte might help.
str_repeat(" ", 256); (or anything else)
I'd like to add that these steps can help solve the issue but from my experience the results are not perfect.
Every new browser and browser version might act different.
So I have a php application with lots of different files, the code is a bit of a mess.
What I want to do is install the application on my server, use it briefly, try to cover all functionality clicking here and there, and then get a log somehow that says 'these php files were used' or 'these functions were accessed'.
I don't care about function arguments, time needed, how many times each thing was called etc etc.
I just want to see if there are functions or at least whole files that are never called - so they are dead.
I know that
1) it could be possibly done by editing each file and writing something out when it is called or included
2) there may be static tools that try to follow all code paths etc.
I'm not interested in the above - i need just something, probably a php module/debugger I'm not sure, that logs which files are requested/included/run by php.
If specific functions can be traced too, even better. But even just files would be awesome.
Sound like you are looking for debugger like xdebug, specially this feature and this function trace
This is just to get a list of files that are in use. A debugger would be required to do a profiling of your project. Also reference this answer to a similar question.
The list of included files will be saved to a text file.
In your php.ini, set up auto_prepend_file to point to a file that contains the following.
<?php
function shutdown_file_output()
{
file_put_contents( 'included_files.txt', implode( PHP_EOL, get_included_files() );
}
register_shutdown_function('shutdown_file_output');
PHP's debug_backtrace() function can be used to get a full trace of a script's execution, including called functions and arguments. If you place a call to this function at the end of your script and log the output to a file or files then that should accomplish what you're looking for.
What is faster on opening and proccecing:
Having under 1 file all the jquery functions or
Each function to a separate file and call it when ever you need it?
Ex. I have a blabla.js file that has 4 functions in it.
And my xaxa.php that calls the blabla.js.
Now, when I firstly open my page its fast enough. No problem (even with cookies cleared and all)
BUT... when I first (and after) click a button that activates a part of my blabla.js all my links and functions are opening/working slower.
So should I separate my functions and load each js file where ever I need it or my problem is somewhere else?
Thank you
(As I said I start to suspect something in my structure)
So here is a sample of my jq:
$(document).ready(function(){
$(".avoid_ref_add").click(function(){
var keyValues = {
pid : $(this).parent().find('input[name="pid"]').val()
};
$.post('help_scripts/cartfunct.php', keyValues, function(rsp){
$('#content').load("p_body.php");
});
return false;
});
function remove
function update
});
and I have my items.php items2.php items3.php...
Now, MY COOKIES I HAVE THEM CLEARED NO CACHE... When I firt open the site, it loads fast and all links are fast...
But if I click that add button everything start to work REALLY slow...
IF I just refresh the whole page it starts working fast again and so on...
FOR ME is quite strange and I cannot figure what I did wrong... Because if the page was slow, it wouldn't load fast from the first time... Correct? Is it something in my code?
You should optimally put all your javascript in one javascript file.
This file could be served with gzip compression and far-future expiry headers to limit bandwidth usage (and result in faster pageloads). You could even run a minimizer on your javascript to reduce file size.
What you are really asking for is the art of minimalization/optimisation and this article is a good read: http://developer.yahoo.com/performance/rules.html
It is clearly better to hold all your JS functions in a single file and this should be faster. Think that when a different function needs to be called you want to avoid the delay inserted by the hit to the server
Execution speed should not be affected by the way you distribute functions among the files. However, download of all resources related to a page is faster when fewer files need to be downloaded. That's because each HTTP requests comes with a HTTP request header and each HTTP response come with a HTTP response header. If the keep-alive feature of HTTP is not used, additional overhead comes from establishing a TCP connection for each request.
You don't really have to separate each function to a separate file. I would suggest keeping the functions that are related to each other in the same JS file and putting others in another JS file.
This makes it easier for you to manage the code and increases the reusability/modularity of the JS file.
After that, you can use minify http://code.google.com/p/minify/ to combine the necessary JS files when you need to, thus reducing the number of requests made to retrieve the JS files.