How to use relative paths for included files such as .css

后端 未结 8 1312
梦如初夏
梦如初夏 2020-12-29 15:07

I have a header.php file which contains a .css file link.

When I \"include\" header.php into another php file in different fo

8条回答
  •  没有蜡笔的小新
    2020-12-29 15:24

    I can see one reason for wanting to have dynamic and relative path generation for href links, and that is if you run your project on multiple domains or sites that have different paths. (For example, your project is available on http://myproject.example.org/ and also on http://example.org/myprojecttest/). If this is not the case, I would suggest directly specifying your css includes relative to the root folder:

    
    

    If this does apply to you, try this:

    In every top-level document that requires header.php, add a $ROOT variable that indicates the top-level document's location compared to the root. e.g.:

    $ROOT = './';
    

    or

    $ROOT = '../';
    

    or

    $ROOT = '../../';
    

    Now, in your header.php file, you can use:

    
    

    This allows you to make a header.php file that will work for any page at any relative path.

    Full Example

    Included File (/path/header.php)

    
    
        
    [...]
    

    File 1 (/path/index.php):

        
    

    File 1 (/path/admin/index.php):

        
    

    File 3 (/path/admin/test/magic.php):

        
    

提交回复
热议问题