Include A File From Document Root In Php


by Balazs Hende

Sometimes it is easy to lose your way while coding your website. It happens when you are tired, preoccupied, or simply snowed under. The best example is a simple html website that provides path to content files. Coding and including files is really easy in html because you can simply use relative path. The same method does not work in php.

The "include filename.extension" code should be used in php to include a file. It works without any problems if the included file is in the same path with the includer file. The problem appears when you use subdirectories to contain the different codes for your website. If you forget to provide absolute path to your "filename.extension" then you receive a blank result. It is evident that the code does not work correctly, at least not in every case.

The worst option is to try and correct the code without any backup, or to start it all over from a new direction. Take a deep breath, think the code over and the solution will come.

The best solution is to echo the server document root: this will locate your code and show which files you should include. You are bound to get satisfactory results after providing the correct parameters and the correct path. This method is great because the includer path and the included filename can be printed even if the directory listing is disabled.

There are two reasons why disabling the print function of the server document root is a priority: it looks bad if it appears on your website and there are security issues. Your website is vulnerable if the server resources can be viewed by visitors, as not all visitors are friendly.

The best protection against nosy visitors is the "htaccess" code used with a "htpasswd": visitors can access the website if they know the username and the password. Using this method keeps your website hidden from search engine crawling bots too. This method is great in the development stage.

Php is an easy language to learn because it is open-source and you can find solution for almost everything if you search the internet. Including a file from server document root in php requires basic knowledge: it should be used as it provides a good foundation to your website if subdirectories are employed. Personally I would recommend using absolute path every time, as relative path is only useful in html.

About the Author

This article is written by Balazs Hende. Balazs is the owner of http://sparesome.com and he is passionate about coding in php.

The source of this article is How to Include a File from Document Root in PHP

Tell others about
this page:

facebook twitter reddit google+



Comments? Questions? Email Here

© HowtoAdvice.com

Next
Send us Feedback about HowtoAdvice.com
--
How to Advice .com
Charity
  1. Uncensored Trump
  2. Addiction Recovery
  3. Hospice Foundation
  4. Flat Earth Awareness
  5. Oil Painting Prints