Weborum Webmaster Forum > [Beginner] SSIs with PHP
Help - Search - Members - Calendar
Full Version: [Beginner] SSIs with PHP
Weborum Webmaster Forum > TUTORIAL ARCHIVE - tutorials & scripts to save you scouring the internet. Please feel free to add your own. > PHP Tutorials & scripts
Timo
This tutorial is on using server side includes in php. You do not need MySQL to use this tutorial.

What are SSI?
Well server side includes are files which are included server side when a client asks for a document.
These files are useful for many reasons... I'll go into detail below.

What are the advantages of using SSI?
  • Updating the site as a whole is easier. (You don't need to edit every file.)
  • Allows "template" files to be created. (You can create a new page on your site in virtually seconds.
  • The client doesn't need any new software, it's on the servers side.

What are the disadvantages of using SSI?
  • The user never caches the information because the server grabs it every time. (To my knowledge.)
  • Usually there is no noticable difference in speed, but a slow server it could probably be seen on.
  • If your layout is somewhat fragile, sometimes different sized main elements can mess it up because it's the same code, not unique to the page, tables can fix this though.

Is your way the only way to do it in PHP?
My way is the easiest way and it works in every version that supports includes (I think they all do.) but it's not the only way... Why do extra work though?
Yes, you could also stream the file in (a technique useful if you want to stream your
livejournal or such in and want to cut off the pages html, head, and body tags.).
There are probably other ways too, this is just one way, some servers also don't allow
you to stream files, yet all allow this (to my knowledge).

What is the php include function?
Yes! Include is just a function. All it does is grab the file that is placed within it and include it into the processing.
Let's assume you grab the file header.php...

Your code could look like the following
CODE

<?php
echo 'hi';
$there = 1;
include 'header.php'; #The include function, also works as include ('header.php'); same difference...
?>

Now let's assume your header.php file includes the following:
CODE

<?php
if ($there == 1) # '==' means is equal to, conditional statement
echo '<br />Hi mom!'; #only displayed if $there == 1...
?>

If we save the files and open up header.php alone we will get no output... This is because in header.php $there == NULL not 1 because it was never set.

If we are to open up that first file we created which includes header.php we find that it will display
CODE

hi
Hi mom!


Why does this work?
Simple the server side program flows through commands from top to bottom. It will combine all the included statements into the calling file so anything set previously, like out $there variable, will be carried over.


Alright, I understand, but how would I find use in this?
Simple! Let's make ourselves a little "template!" For this we will use my website's design and I will show you how I would split it up.

First let's take a look at my page...
user posted image
Ok, analyzing this image we see that there are pretty much 3 sections (note you could have hundreds of sections if you wanted, but that would kind of take away any real use of includes... I'd try and keep it to 4 or 5 tops unless you do need more.
The three sections are the top section which includes the banner and the buttons, the main text area section which includes the unique content we display per page, and a footer section which includes our sidebar and my sitemap, copyright section.
Let's cut it up then.

user posted image

Alright, we will call the top section header, and the bottom section the footer.
Hold on! My title changes per page! I can't do this!
I told you my little variable trick would prove useful... Here's what our template will look like first, then I'll explain it in more detail...
CODE

<?php
$title ="This is my homepage dawg"; #our title
include 'header.php'; #our header include
?>
Yo this is where my main content goes, anything here will go in that space I left open.
<?php
include 'footer.php'; #our footer include
?>


Alright, that is all that there is to the template. The header tag contains all the html from the first statement to the beginning of the main area and the footer contains whatever html we had from the tag right after our main area to the html closing tag.
Alright, but now what about the title?
Simple, just do the following... where your title tag is located...
CODE

<title><?php echo $title;?></title>

Simple huh? If you have any questions feel free to post them.
Joe
ok, i got a question (as always)

say my whole page is now:

CODE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<title>My Page Title</title>
<style style="text/css">
some CSS here
</style>
</head>

<body>
content content content content
</body>
</html>
to split it up, page 1, header.php

header.php
CODE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<title>My Page Title</title>
<style style="text/css">
some CSS here
</style>
</head>


footer.php
CODE
</body>
</html>


and then my main page where all this is to be put together would be

index.php
CODE
<?php $title ="My Page Title"; include 'header.php';?>

[i]content content content content[/i]

<?php include 'footer.php'; ?>
now would this be correct?
Timo
QUOTE (joe2kiss @ Mar 14 2004, 04:19 PM)
ok, i got a question (as always)

say my whole page is now:

CODE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<title>My Page Title</title>
<style style="text/css">
some CSS here
</style>
</head>

<body>
content content content content
</body>
</html>
to split it up, page 1, header.php

header.php
CODE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<title>My Page Title</title>
<style style="text/css">
some CSS here
</style>
</head>


footer.php
CODE
</body>
</html>


and then my main page where all this is to be put together would be

index.php
CODE
<?php $title ="My Page Title"; include 'header.php';?>

[i]content content content content[/i]

<?php include 'footer.php'; ?>
now would this be correct?

header.php

header.php


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<title><?php echo $title;?></title>
<style style="text/css">
some CSS here
</style>
</head>
<body>

You forgot the openning body. And in order for the title trick to work you need to change the title to above...

Footer is right.
And template looks good.

cowboy.gif Yep! :-D

-Tim
Josh
Well joe, you don't really need to include every part of your page. It's best to just include things that will change or ar enot the same on every page.
Joe
well so i would only need the header really wouldnt i, thanks Timo

going to make that offline PHP version of my site you suggested
Josh
That's what I thought the first time I used php includes. I thought I would just include everything. But nope, check out this thread in htmlforums for an example that rydberg wrote of how it can be used:
http://www.htmlforums.com/showthread.php?s...&threadid=32364
Timo
QUOTE (Josh @ Mar 14 2004, 04:32 PM)
That's what I thought the first time I used php includes. I thought I would just include everything. But nope, check out this thread in htmlforums for an example that rydberg wrote of how it can be used:
http://www.htmlforums.com/showthread.php?s...&threadid=32364

It does it backwards to the way I do it... I don't suggest his way. Google and other pages have problems with complete dynamic pages.
I tend to do it halfway on my pages.

I really suggest you do it my way for search engine safety.

But it's up to you, again, I'd find his way easier if I planned on changing the order of my includes and such a lot, but my way is safer and stronger if you're coding for google and such.
Josh
yah, but google won't pick up each individual file that's included, it will pick it up as a whole right?
Timo
QUOTE (Josh @ Mar 14 2004, 04:46 PM)
yah, but google won't pick up each individual file that's included, it will pick it up as a whole right?

Err no, what I mean is...

The way his script works is like this...

index.php?page=games
index.php?page=tutorials
index.php?page=otherstuff

Google may go into those links but it will have trouble with them and many other seach engines just ignore those all together.
So all you'll be showing is the index.php page, or in his scripts case the default text. All of the includes will not be included besides the default.

The way my templates work is you have
index.php
games.php
tutorials.php
otherstuff.php
So all of them work.

Dynamically created pages, his in this case, aren't bad, but using only them isn't smart if you want good search engine ranking.
bassrek
QUOTE (Timo @ Mar 14 2004, 11:17 AM)
Dynamically created pages, his in this case, aren't bad, but using only them isn't smart if you want good search engine ranking.

Amen to that, brother. I do all my real work on an intranet and have been using a system similar to Rydberg's for several years, and it's great. Unfortunately, I've done it on 2 external sites and like you say, timo, it's a pain for the SEs. It's a pain, too, to get usable traffic stats doing it that way, too (unless you have a good tracker like whatcounter </suckup>).
leo
laugh.gif thumbsupsmileyanim.gif

a way around long query strings and session id's is to use mod_rewrite to rewrite the urls smile.gif

working on a tut for it smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.