Server Side Includes Using PHP

08/04/07

Permalink 09:03:23 pm, by admin Email , 683 words, 296 views   English (US)
Categories: Web Design

Server Side Includes Using PHP

(when the file is in the same directory),

or

(when the file is in a different directory on the same server)

In this case, if you wanted to include the file "nav-bar.html" in another file called "product.html", you would normally have to change the extension to something like .shtml. So it would look like this:

product.shtml

Using "includes" across domains

But what if you wanted to include a file within a page when the include file resides on a different server? You cannot do this with normal SSI. But you can do it with PHP.

For example, say you wanted to include an article published on someone else's site in a page of your own. A few years ago we might have used frames to accomplish this. But now through the miracle of PHP, we can do it by simply adding a line of code like this:

Here is an example of a page where I have used this exact include. Notice that I must give the new file a .php extension.

Rick's version of the Microsoft home page

Pretty cool, eh!

Unlike with frames or java or most other methods of including content from other sites, using PHP makes you page look like the original. All the source code looks just like it does when you look at the original page.

Practical Applications

Doing something like I have done in the above example -- embedding someone else's content in your own pages -- is not likely going to happen very often. There's usually no point.

But here are two applications of this technique that I use all the time:

Embedding the same content in more than one website

It has become very popular to create mini-networks of sites for promotional and search engine marketing purposes. Using this PHP include technique allows you to take the same content and use it in a number of sites. For instance, say you have a common FAQ that you want to use in a number of sites. As long as you are happy with having the same information in each included version, this technique works well. You can even format it differently from site to site.

Embedding RSS feeds

If you use RSS feeds to create content, the PHP include technique is awesome. For instance, I use a feed parser called CARP to create a formatted version of feeds from MSN Search. I place all the original feeds on the server where CARP is running. Then when I want a feed for a particular search, I just do an include referencing the original file. Here is an example of a page that includes the MSN search results for the search words "Civil Rights":

MSN Search for Civil Rights

The code for the include is:

Try embedding this in a page on your own site. Make sure to give it a .php extension.

Rick Hendershot publishes the Linknet Publishing Network. The network includes advertising services as well as an
Article Promotion Program
.

*PHP: Hypertext Preprocessor is an open source server side programming language extensively used for web scripts and to process data passed via the Common Gateway Interface from HTML forms etc. PHP can be written as scripts that reside on the server and may produce HTML output that downloads to the web browser. Alternatively, PHP can be embedded within HTML pages that are then saved with a .php file extension. The PHP sections of the page are then parsed by the PHP engine on the server and the PHP code stripped out before the page is downloaded to the web browser

-->

by Rick Hendershot, Linknet Publishing Network

One very useful capability of PHP is its ability to handle Server Side Includes (SSI) across domains. PHP is usually installed on your web server, so you probably have access to its capabilities whether you know it or not.

If you use a Linux based web server, chances are very good you have access to the impressive capabilities of PHP*. These capabilites often involve databases and creating dynamic web pages from information stored in a database.

If you don't know what that means, don't worry about it. Like most article sites, this one (free-web-tools.biz) is dynamically assembled from information stored in a database. It is a prime example of the capabilites of PHP.

Another very useful capability of PHP is the way it can handle Server Side Includes (SSI) across domains.

Using includes within a domain

When would you normally use a server side include?

One standard use is when you have a common element that you want to appear on a number of pages within your site. A menu or navbar for instance. SSI lets you create one master file for the menu and then "include" it in any number of pages simply by including a bit of code in the appropriate place.

The standard syntax for non-PHP based SSI is as follows:

(when the file is in the same directory),

or

(when the file is in a different directory on the same server)

In this case, if you wanted to include the file "nav-bar.html" in another file called "product.html", you would normally have to change the extension to something like .shtml. So it would look like this:

product.shtml

Using "includes" across domains

But what if you wanted to include a file within a page when the include file resides on a different server? You cannot do this with normal SSI. But you can do it with PHP.

For example, say you wanted to include an article published on someone else's site in a page of your own. A few years ago we might have used frames to accomplish this. But now through the miracle of PHP, we can do it by simply adding a line of code like this:

Here is an example of a page where I have used this exact include. Notice that I must give the new file a .php extension.

Rick's version of the Microsoft home page

Pretty cool, eh!

Unlike with frames or java or most other methods of including content from other sites, using PHP makes you page look like the original. All the source code looks just like it does when you look at the original page.

Practical Applications

Doing something like I have done in the above example -- embedding someone else's content in your own pages -- is not likely going to happen very often. There's usually no point.

But here are two applications of this technique that I use all the time:

Embedding the same content in more than one website

It has become very popular to create mini-networks of sites for promotional and search engine marketing purposes. Using this PHP include technique allows you to take the same content and use it in a number of sites. For instance, say you have a common FAQ that you want to use in a number of sites. As long as you are happy with having the same information in each included version, this technique works well. You can even format it differently from site to site.

Embedding RSS feeds

If you use RSS feeds to create content, the PHP include technique is awesome. For instance, I use a feed parser called CARP to create a formatted version of feeds from MSN Search. I place all the original feeds on the server where CARP is running. Then when I want a feed for a particular search, I just do an include referencing the original file. Here is an example of a page that includes the MSN search results for the search words "Civil Rights":

MSN Search for Civil Rights

The code for the include is:

Try embedding this in a page on your own site. Make sure to give it a .php extension.

Rick Hendershot publishes the Linknet Publishing Network. The network includes advertising services as well as an
Article Promotion Program
.

*PHP: Hypertext Preprocessor is an open source server side programming language extensively used for web scripts and to process data passed via the Common Gateway Interface from HTML forms etc. PHP can be written as scripts that reside on the server and may produce HTML output that downloads to the web browser. Alternatively, PHP can be embedded within HTML pages that are then saved with a .php file extension. The PHP sections of the page are then parsed by the PHP engine on the server and the PHP code stripped out before the page is downloaded to the web browser

PermalinkPermalink    

Pingbacks:

No Pingbacks for this post yet...

Web Magic

Articles about Web Design, Web Programming, Techniques to make webs more efficient, using CSS, using images, and much more.

Misc

Who's Online?

  • Guest Users: 15

powered by
b2evolution