Organize items by including files

Source: Internet
Author: User

The include () statement gives the PHP programmer a powerful project management tool. Putting some content or layout ideas into inclusion files can save you countless hours of development time.

(in PHP programming) at least two generic include files, known as generic, that must be referenced (or included) at the top of any PHP page. The main effect of these two references is that I don't have to embed or encode domain names, paths, and links in my program. I can easily migrate my project from my development and development machine to the product server.

Include file: config.php

config.php files are used to save database connection details and other settings

<?phpdefine (' db_host ', ' localhost ');d efine (' Db_user ', ' Jatinder ');d efine (' Db_password ', ' secret ');d efine (' Db_database ', ' xyz ');d efine (' TFX ', ' xyz_ ');d efine (' Server_url ', ' http://localhost ');d efine (' app_fol ', '/phpsense/') );? > The first line we define the connection details for the future using the mysql_connect () and mysql_select_db () functions. Then we define a TFX constant used as the standard prefix for the data table. Add this prefix to all tables in the database. The table prefixes for each item are unique. Even if you know to install a new clean piece of data into your project, it doesn't prevent you from adding a prefix. If you are developing a project called Phpsense on the local machine, in most cases the PHP file will be in the Htdocs directory under the "Phpsense" folder. The URL to your project will look like http://localhost/phpsense/I divide this path into two parts: Server_url and App_fol (program folder) when I ported items from my development machine to a product server, I just change the Server_url (server path) to the site domain name (for example: http://phpsense.com) will app_fol from "/phpsense/" into "/". app-top.php contains files in this include file we initialize our program: 1. Turn on the cache output 2. Open the database connection 4. Define other common 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);
?>

In addition to opening the session and connecting to the database, the app-top.php script also defines a pair of constants, which are the basic paths or URLs I need to go to various folders, such as Includes_dir, which stores the path address of the containing file. Therefore, is not used: <?phpinclude ("includes/myscript.php");

?> I use: <?phpinclude (Includes_dir.) Myscript.php ");

?> now I don't have to worry about my relative address, in addition I can rename my include folder without interrupting the program. Do not use hard-coded paths and URLs in your program, using similar constants like this. Now all you have to do is quote app-top.php at the top of each PHP page, and any page that contains this file will have a session, cache output, database connections, and predefined paths to include files and libraries. Note: You do not need to refer to app-top.php in other include files, just refer to it at the top of the page. Why are two files you might ask why I used two separate files and I could easily make both. The answer is to get more flexibility in doing so. Suppose a client asks me to create an installation script for the program, now I have to create a simple form and write the value to the config.php file, and writing the value to config.php in PHP is simpler than writing app-top.php.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.