13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

142 Chapter 5 Reusing Code <strong>and</strong> Writing Functions<br />

This approach gives you a consistent-looking website very easily, <strong>and</strong> you can make a<br />

new page in the same style by typing something like this:<br />

<br />

Here is the content for this page<br />

<br />

Most importantly, even after you have created many pages using this header <strong>and</strong> footer,<br />

you can easily change the header <strong>and</strong> footer files.Whether you are making a minor text<br />

change or completely redesigning the look of the site, you need to make the change<br />

only once.You do not need to separately alter every page in the site because each page is<br />

loading in the header <strong>and</strong> footer files.<br />

The example shown here uses only plain HTML in the body, header, <strong>and</strong> footer.This<br />

need not be the case.Within these files, you could use <strong>PHP</strong> statements to dynamically<br />

generate parts of the page.<br />

If you want to be sure that a file will be treated as plain text or HTML, <strong>and</strong> not have<br />

any <strong>PHP</strong> executed, you may want to use readfile() instead.This function echoes the<br />

content of a file without parsing it.This can be an important safety precaution if you are<br />

using user-provided text.<br />

Using auto_prepend_file <strong>and</strong> auto_append_file<br />

If you want to use require() or include() to add your header <strong>and</strong> footer to every<br />

page, you can do it another way.Two of the configuration options in the php.ini file are<br />

auto_prepend_file <strong>and</strong> auto_append_file. By setting these options to point to the<br />

header <strong>and</strong> footer files, you ensure that they will be loaded before <strong>and</strong> after every page.<br />

Files included using these directives behave as though they had been added using an<br />

include() statement; that is, if the file is missing, a warning will be issued.<br />

For Windows, the settings look like this:<br />

auto_prepend_file = "c:/Program Files/Apache Software<br />

Froundation/Apache2.2//include/header.php"<br />

auto_append_file = "c:/Program Files/Apache Group/Apache2/include/footer.php"<br />

For Unix, like this:<br />

auto_prepend_file = “/home/username/include/header.php”<br />

auto_append_file = “/home/username/include/footer.php”<br />

If you use these directives, you do not need to type include() statements, but the headers<br />

<strong>and</strong> footers will no longer be optional on pages.<br />

If you are using an Apache web server, you can change various configuration options<br />

like these for individual directories.To do this, you must have your server set up to allow<br />

its main configuration file(s) to be overridden.To set up auto prepending <strong>and</strong> appending<br />

for a directory, create a file called .htaccess in the directory.The file needs to contain<br />

the following two lines:<br />

php_value auto_prepend_file “/home/username/include/header.php”<br />

php_value auto_append_file “/home/username/include/footer.php”

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

Saved successfully!

Ooh no, something went wrong!