Hi, Habr!
Today I would like to introduce new webmasters to a variety of elegant ways to use output buffering in php. Experienced webmasters find out for themselves whether they find something useful. Although - who knows?
As you all know, output buffering in php is controlled by a set of functions starting with ob_. The most important of them is ob_start. When launched, it collects the subsequent output, that is, all sorts of print (), echo, etc., which then will be given to the visitor in the form of an html page. And if, before displaying, we started buffering, then with this page, which is almost ready, you can finally create something.
')
For example, we want to filter all links to foreign sites.
On our forum, ancient as the ax of Australopithecus, a great many spammers are swarming, luring the visitor to places filled with debauchery, one-armed gangsters and political agitation. You could use js tracking, but we want to change all of these links instead like this:
"http://blackjack-hookers.com" => "http://myoldforum.ru/redirect.php?url=blackjack-hookers.com"
The method may not be the most effective, but effective. We wrote a redirect.php with a filter and a black sheet, and now we need to convert all the links on thousands of forum pages. With ob_start and a couple of regular expressions, we'll do it in just a few lines:
function f_callback($buffer){ $buffer = preg_replace('#http://(www.)?myoldforum\.ru/#','/',$buffer); $buffer = preg_replace('#href="http://([^"]*)"#','#href="/redirect\.php\?url=$1',$buffer); return $buffer; } ob_start(f_callback);
Now, by connecting this code at the beginning of index.php, or another file accessed by the server when browsing, we will get what we need.
Changing the content in this way, we are not limited to the scope of the engine methods. This is very valuable. You can, for example, add a plugin:
function generate_plugin(){ } function f_callback($buffer){ $buffer = str_replace ('<!--plugin-set-->',generate_plugin(),$buffer); return $buffer; } ob_start('f_callback');
Now, where we added to the content will appear what we wanted to get. One of the applications is to insert a js widget on a site page. For example, Yandex cards. Usually it is not difficult, but sometimes crookedly written editor of the site pages shields quotes and curly brackets, breaking the widget. As you can see, this problem is easily solved.
The php toolkit for working with the output buffer is rich and is not limited to only ob_start. The above techniques in some cases are too resource-intensive and cumbersome, since they operate on the entire page. We can process only a part of it, creating a shell around the generation of something in the template, which we don’t want to climb into the wilds, but we need to fix it:
<? ob_start(); ?> {GENERATE BIG CRAZY THING} <? f_callback(ob_get_clean); ?>
You must have already noticed all these turns: “I don’t want to crawl”, “as ancient as a tyrannosaur chair”, “crookedly written editor” ... In the ideal world, shells around the output buffer are not needed. Everything that can be done with ob_start could theoretically be done without it. This technique sometimes contributes confusion to the project code, many see its meaning only in that it would give the output to ob_gzhandler for compression, and consider its use in other cases dangerous. But often, without the management of the output simply can not do.
Especially if you do not want to dig deep.