PHP is the ultimate solution to solve the Path Problem of the HTML code in the referenced path and repeated Referenced File

Source: Internet
Author: User

After studying the reference path of PHP for a while, I came up with this solution.

Create a global. php file in the include folder under the root directory of the website.

Global. php

<? Php <br/> // convert the current directory to the root directory <br/> chdir (dirname (_ file __))); </P> <p> include 'config/config. php'; <br/>?> <Br/>

Create a config. php file in the include folder under the root directory

<? Php <br/> // after this definition, add the website_base constant before the link in HTML <SPAN class = 'wp _ keywordlink '> Code </span> to the absolute link. <br/> // when the domain name or server is changed, modify the following code: <br/> define ('website _ base', 'HTTP: // localhost/getfootballcontent'); </P> <p >?> <Br/>

Then, when each PHP file needs to reference other files, it will first reference global through absolute link. PHP file. This is global. PHP sets the current directory as the root directory, and then the Referenced File only needs to use its relative path relative to the root directory.

For example, to reference db_fns.php in the admin folder under the root directory, use the following method:

Include_once (dirname (_ file __). '.. /include/global. PHP '); </P> <p> require_once ('. /include/db_fns.php '); <br/>

Remember to use require_once to reference the file. This prevents repeated references when multiple files are referenced.

If the HTML code in the PHP file references the Javascript file, you can use the following method:

 

 

In summary, the advantages of my solution are:

1. All files reference the same file (such as the db_fns.php file) in the same path. The difference is that they must use the relative address When referencing the Global File.

2. The address of the HTML code in the PHP file is absolute, so that no matter any file references the PHP file from any location, the URL in the HTML code is correct.

3. When changing the domain name or server, you only need to modify a piece of code in the config. php file.

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.