A solution to the problem of inclusion path in PHP Web development

Source: Internet
Author: User
Keywords A solution to the problem of inclusion path in PHP Web development
Tags php web development

Introduction:

The inclusion path in Perl and PHP has always been a difficult problem, mainly related to the operating system and Web server, it is impossible to solve this problem very intelligently. The path relative to perl,php is much better and easier to solve, because the relative path of PHP can be used anywhere in the PHP program, unlike Perl's need to use absolute paths in certain statements to make porting extremely complex.

Based on this, in PHP, I designed an absolutely secure solution, as described below.

Principle:

Use relative path, but in relative path with absolute path (a bit around, to be explained later), one can guarantee portability, second, can be easily modified, three is formulated and the structure is clear, easy to expand.

Detailed steps:

1, first determine the root of a program, note that the file system, not the virtual directory under the Web server, but in general, the directory under the subdirectory of the relative path to the directory under the virtual subdirectory is the same.

2, under the defined program root directory under each sub-directory (in fact, not necessarily each, Create a settings.php that defines a variable or constant (the constant is better because the scope is larger), such as AppRoot, but this approot is not an absolute path, but rather a relative path to the directory relative to the program root you specify.

3, in this directory all the program entry file (also is the first file containing other files, or allow to browse directly in the browser file) in the first sentence to write require_once (' settings.php '), but note that all included files should not add this sentence-- Actually add also can, because you can write in settings.php the IF (!defined (APPROOT)) define (APPROOT, ' ... /..'); This type of statement prevents redefinition.

4, if you want to include other files, whether directly or indirectly included, can be written as include (APPROOT. $path); Here $path is the absolute path of the containing file relative to the root of the program you specified.

Principle:

The program root is the relative path, but the specific directory location is the absolute path relative to that root directory, the combination of which is the relative path of the specific file relative to the program root directory. For example, the directory C:wwwrootapp for the program root directory you specified, then there are two files c:wwwrootappaindex.php and c:wwwrootappinc.php. In the case of subdirectory A, AppRoot is '. ', and for the program root, the absolute path to inc.php is $path= '/b/inc.php ', which is combined as '. /b/inc.php '. If you want to include inc.php in index.php, you should write an include ('.. /b/inc.php '), and this path is just the combination of the AppRoot. $path?

Conclusion:

After the above processing, each path is absolutely uniform, the only thing to wordy is to define this approot in each directory, but it is sufficient to define each directory only once in the settings.php of this directory. If your entire program has only one entry file, such as index.php, and all other files are included directly or indirectly in this unique portal file, simply define it once in the settings.php directory of the index.php. If a friend has done Delphi Project and study the project documents, you will find that I just said a program only a main entry file and Delphi project very similar, because Delphi in addition to a main program file (DPR file), the rest of the whole is a unit file or resource files, cannot be executed independently. In PHP, if this is the case, simply define the APPROOT once and write the first sentence in the main program file as require_once (' settings.php '), and all subsequent inclusions can be used in the include (APPROOT. $path); Make sure you don't have any problems, unless you don't write this "absolute path containing files relative to the root of the program" $path.

I used this method more than once, and it worked well. You can also refer to how paths are defined in the web-info of the JSP (Preferred for Sun enterprise applications).

I this is status quo formulation plan, if has the friend has the better plan, welcome to propose the discussion! If you do not understand, also welcome the proposed.

  • Related Article

    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.