The include() statement gives PHP programmers a powerful tool to organize their projects. Putting some thought into the content and layout of your include files may save you countless hours of development time.
There should be at least two universal include files. By universal I mean, that they should be included at the top of every PHP page. The main benefit of these two includes files is that I don’t have to embed or hard code domain name, paths and URLs into my application. I can easily shift my project from my development machine to a production server.
config.php include file
confg.php file holds the database connection details and other settings
In the first four lines we define the connection details for use in mysql_connect() and mysql_select_db() functions.
Next we define a TFX constant which stands for table prefix. Always append a prefix to all your table names in the database. The table prefix should be unique for each project. Even if you know that you will get a clean and new database to install your project, it does no harm to add a prefix.
If you are developing a project named phpsense on your local computer, in most cases the PHP files will be in a “phpsense” folder under the htdocs directory. The URL to your project will look like http://localhost/phpsense/
I have split this path into two parts: SERVER_URL and APP_FOL (Application Folder)
When I move the project form my development computer to a production server, I just change the SERVER_URL to the website’s domain (Example: http://phpsense.com) and change APP_FOL from “/phpsense/” to “/”
app-top.php include file
In this include file we initialize our application:
- Start output buffering
- Start session
- Open database connection
- Define other utility variables and constants
print("Failed to establish connection to mysql server!");
The app-top.php script, besides starting session and connecting to the database, defines a couple of constants. These are basically paths or URLs to various directories I might need. For example, INCLUDES_DIR gives me the path of the folder in which I store my include files.
So, instead of using
Now I don’t have to worry about relative paths. Additionally I can rename my includes folder without breaking the application.
Instead of hardcoding the paths and URLs into your application, use constants similar to these.
Now all you have to do is to include app-top.php at the top of each PHP page. Every page which includes this file will have a session, output buffering, database connection and pre-defined paths to includes and lib folders.
Note that you do not need to include app-top.php file in other include files. Only include it in top-level pages.
Why two files
You might ask why I used two separate files when I could have easily combined the two into a single include file. The answer is that this provides me more flexibility.
Suppose a client asks me create installer script for the application. Now all I have to is to create a simple form and write the values to config.php file. Writing values to config.php through PHP will be a lot simpler than writing app-top.php