开发者

How do you use a require_once header for items in a different folder?

I use require_once("header.php"); for all pages of my website. Though, I feel like I can only use that code for files filed directly under the public_html folder. If I use the require_once("header.php"); code for any other files located in a directory (e.g. shirts) under public_html, such that you have public_html/shirts/example.php, the header.php doesn't load the images, links, or any other pages correctly.

I know to resolve this in the short term, I can make a new header just for that directory and add ../ to all the code, but I think there's an easie开发者_Go百科r way. Does anyone know of a way I can just change the code of header.php, so I don't have to make separate headers?

Thanks.


You could add to your include_path.

Either add the current directory where header.php is, or create a folder called includes somewhere in your site and add that to the include path.

You can either update the include_path in php.ini (easiest) or do it programmatically like this:

set_include_path(implode(PATH_SEPARATOR, array(
    '/path/to/your/includes',
    get_include_path(),
)));

The programmatic method works well, but unless most of your files are served through a common script (i.e. front controller pattern) then you will need to copy that code to all of your files which is not ideal. Sometimes you can use .htaccess to set the include_path as well, but only if PHP is an Apache module.

As long as your structure is fairly static, there is nothing wrong with includes using ../ either.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜