SSI(Server Side Include) in PHP

  • September 5, 2012
  • Code
  • SG

SSI is a Server Side Scripting Language used most commonly in many web applications. SSI will help in organizing files and templates for your application. SSI is used to include contents of another file in to a web page on the web server, similar to the include file statement we use in other server side scripting language like PHP etc.

Example : This SSI can be used to implement Navigation items in the website. Say for example If there are 100 pages in your application you dont have to add same navigation across all 100 pages, instead of this all pages can refer to a single navigation file. So if there is a further change in the navigation item ordering or arrangements you can simply edit one file which will reflect across the website. This is pretty simple!

Similarity & Difference : Similar to SSI many template engines like smarty is also doing the same thing the difference is smarty will parse the code and convert it in to HTML but SSI has ability to include the file in HTML code itself.

SSI in Dynamic pages : In Dynamic pages SSI can be implemented in 2 ways

  • Server-Resolved SSI in Script : The way of including dynamic files in the HTML page is same as static pages. Based on the dynamic query and page this method will generate temporary file  for each pages and displays it on web-browsers.
  • SSI Emulation : The program will render the dynamic pages as the web-server follows. There are many SSI tags supported but using this method you can use only “include and virtual”. If you want to make use of other commands(“echo, config, printenv…”) you should use Server-Resolved SSI.

PHP already have include function then how to use SSI includes?

PHP page normally need to use include command rather than regular SSI include. . PHP includes differ in an important way – rather than using the absolute web path like “include virtual” (above), they use the full folder path on the server.

You can include the SSI in two ways,

  1. #include virtual : Includes a file by following the absolute web path to a document on the server. To find the absolute web path, take the full URL of the file and remove the domain name (and http://). Include virtual is compatible with both Server-Resolved SSI and SSI Emulation.
  2. #include file : Includes a file by following a path relative to the current directory. Include file is compatible with Server-Resolved SSI but not with SSI Emulation.

HTML version of implementing SSI :

1. include virtual : To include a file located at ““, the following #include virtual would be used:

2. include file : Below are some examples of #include file, using relative paths. For example, if the file “footer.html” is in the same folder as the HTML page we’re working with, you’d use the following:

If the copyright.txt file is in the “includes” folder (a subfolder of our current directory), we’d use this file include:

Note: On Unix/Apache servers, include file can’t use absolute paths, or go up to parent (higher) directories with “../”. If you need to refer to a file in a parent directory, you might consider using include virtual instead.

If you want to use SSI with PHP refer this link