Server Side Include (SSI) in PHP
Many a times we put the reusable codes in a separate PHP file. We, then include this file in any other file
where required. This provides an excellent way to maintain a system. When we include a file, we can assume
that the source code of that file copied at place of inclusion as it is while runtime.
The syntax to include a file is as below
<?php include ‘includefile.php’; ?>
We often use the relative path of the file to be included.
Here while including the files, it should be kept in mind that no file can include itself. That is the file
inclusion should not be recursive. That will result in a fatal error and the program will be terminated by the
PHP parser. A typical illustration of such include is explained below.
Let’s suppose that we have two files A and B. If file A includes file B and file B also includes file A,
then that will be a recursive include.
When should we use PHP includes
Some practical examples/ scenario where PHP includes are commonly used are as below.
- Writing the footer in a separate file footer.php and including this file in every other file. This
way we ensure that the footer is displayed on all the web pages. Also if we want to modify the footer any
day, then we will have to modify only the footer.php - Declaring all the static messages in a separate file “message.php” and including this file in
whichever file we need to display message. - Declaring all the userids and passwords to required for connecting to your mysql database in a
separate fie “secret.php”. We can then include this file in every file where we need to connect to the
database. This way we can change the passwords frequently and update the file “secret.php “. Also if you
export your source code to somewhere else, you will be required to modify this secret.php only!
- 453 reads
