The include() statement gives PHP programmers a powerful project management tool. Putting some content or layout ideas into include files can
save
you countless hours of development time .(In PHP programming) There are at least two common include files. The so-called universal means that they must be quoted (or included) at the top of any PHP page.
The main purpose ofthese two reference files
is that I don't have to go through the trouble of embedding or encoding domain names, paths, and links in my programs. I can easily port my projects from my development machine
to a production server.
Included file: config.php
The config.php file is used to save database connection details and other settings
<?phpdefine('DB_HOST','localhost');define('DB_USER','jatinder');define('DB_PASSWORD' ,'secret');define('DB_DATABASE','xyz');define('TFX','xyz_');define('SERVER_URL','http://localhost');define('APP_FOL',' /phpsense/');?>In the first line we define the connection details for future use of the mysql_connect() and mysql_select_db() functions. Then we defined a TFX constant to be used as the standard prefix of the data table. Add this prefix to all tables in the database. The table prefix is unique for each project. Even if you know to install a new clean data into the project, this does not prevent you from adding a prefix. If you are developing a project called phpsense on your local machine, in most cases the PHP files will be in the htdocs directory under the "phpsense" folder. The URL to your project will look like http://localhost/phpsense/ I split this path into two parts: SERVER_URL and APP_FOL (program folder) when I port the project from my development machine to the production server , I just changed the SERVER_URL (server path) to the domain name of the site (for example: http://phpsense.com ) and changed APP_FOL from "/phpsense/" to "/". app-top.php include file In this include file we initialize our program: 1. Enable cached output 2. Open session 3. Open database connection 4. Define other shared variables and constants <?phpob_start('ob_gzhandler');
session_start();
error_reporting(E_ALL);
require_once('config.php');
define('APP_URL',SERVER_URL.APP_FOL);
define('SERVER_DOC_ROOT',$_SERVER['DOCUMENT_ROOT']);
define('APP_DIR',SERVER_DOC_ROOT.APP_FOL);
define('INCLUDES_DIR',APP_DIR.'includes/');
define('LIB_DIR',APP_DIR.'lib/');
define('UPLOADS_DIR',APP_DIR.'uploads/');
define('UPLOADS_URL',APP_URL.'uploads/');
$link=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!$link) {
print("Failed to establish connection to mysql server!");
exit();
}
$status=mysql_select_db(DB_DATABASE);
?>The app-top.php script, in addition to opening the session and connecting to the database, also defines a pair of constants. They are the basic paths or URLs I need to go to various folders. For example, INCLUDES_DIR gives me the path to store the included files. address. So, instead of using: <?phpinclude("includes/myscript.php");
?>I use: <?phpinclude(INCLUDES_DIR."myscript.php");
?>Now I don't have to worry about my relative addresses, plus I can rename my containing folder without interrupting the program. Don't use hard-coded paths and URLs in your program, use constants like this. Now all you have to do is reference app-top.php at the top of every PHP page. Any page that includes this file will have a session, cached output, database connection and paths to predefined include files and libraries. Note: You don't need to reference app-top.php in other include files, just at the top of the page. Why two files you may ask, why did I use two separate files when I could have easily combined the two into one. The answer is that you gain more flexibility by doing so. Suppose a client asked me to create an installation script for a program. Now I have to create a simple form and write the values to the config.php file. Writing the values to config.php via PHP is easier than writing app-top.php.