Me You Site Home

PHP Includes

PHP includes are an easy way to seperate your pages into different parts so you could easily update each part without going through all your pages. They're useful for div layouts and things at the top or bottom of your pages that you might occassionally change.

Headers and Footers

There are 3 parts to a basic .php page: header, your content, and footer. The header contains your <body> tag and all of the HTML before it. The content page is whatever is between the body tags. The footer contains your </body> tag and all of the HTML below it.

Examples

Here's an example of an HTML code for a basic page:

<html>
<head>
<title>My Site</title>
</head>
<body>
<h1>Welcome</h1>
Hi, welcome to my site :D
Back | Home | Forward
</body>
</html>

Here's what you would put in the header (header.php):

<html>
<head>
<title>My Site</title>
</head>
<body>

Here's what you would put in the page (page.php):

<h1>Welcome</h1>
Hi, welcome to my site :D

Here's what you would put in the footer (footer.php):

Back | Home | Forward
</body>
</html>

Keep in mind that header.php and footer.php will be on every page that has includes. So if you want something like a back link or a copyright message on every page, you should put it in the footer. If you want something at the top of every page, put it in the header. Sidebars should also go in the header.

Adding Includes

Add this code to the top of page.php. Replace "user" with your FTP username. public_html probably stays the same but if the folder where all your files are is called something else, edit public_html as well. Also remove the asterisk (*).

<*? include("/home/user/public_html/header.php"); ?>

Add this code to the bottom of page.php. Edit the same things that you did for the header code.

<*? include("/home/user/public_html/footer.php"); ?>

Now save page.php and upload all 3 pages. Test it to see if it works by going to yoursite.com/page.php. It should look like a normal HTML page. If you're using divs, you'll need to add your div code to header.php for it to show like it does normally.

Change all of your pages to look like page.php and add the includes to the top and bottom of the pages. When you're done remember to save them as pagename.php instead of pagename.html.

Need more help with a tutorial?
Contact me.