05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

web server and clears the buffer, but doesn’t terminate output buffering. The flush( )<br />

function not only flushes and clears the output buffer, but also tries to make the web<br />

server send the data to the browser immediately. The ob_end_flush( ) function sends<br />

the output data to the web server and ends output buffering. In all cases, if you specified<br />

a callback with ob_start( ), that function is called to decide exactly what gets<br />

sent to the server.<br />

If your script ends with output buffering still enabled (that is, if you haven’t called<br />

ob_end_flush( ) or ob_end_clean( )), <strong>PHP</strong> calls ob_end_flush( ) for you.<br />

The following code collects the output of the phpinfo( ) function and uses it to determine<br />

whether you have the PDF module installed:<br />

ob_start( );<br />

phpinfo( );<br />

$phpinfo = ob_get_contents( );<br />

ob_end_clean( );<br />

if (strpos($phpinfo, "module_pdf") === FALSE) {<br />

echo "You do not have PDF support in your <strong>PHP</strong>, sorry.";<br />

} else {<br />

echo "Congratulations, you have PDF support!";<br />

}<br />

Of course, a quicker and simpler approach to check if a certain extension is available<br />

is to pick a function that you know the extension provides and check if it exists. For<br />

the PDF extension, you might do:<br />

if (function_exists('pdf_begin_page'))<br />

To change all references in a document from http://www.yoursite.com/ to http://<br />

www.mysite.com/, simply wrap the page like this:<br />

<br />

Visit our site now!<br />

<br />

Visit our site now!<br />

Another way to do this is with a callback. Here, the rewrite( ) callback changes the<br />

text of the page:<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!