简体   繁体   中英

php include - what's wrong with my code?

I am using *.tpl files of html and php code to include into various webpages on my website to fill in common spaces such as header and footer.

I am trying to include and print the code at the correct place in my website. I am using the following:

<?php
include ($_SERVER['HTTP_HOST'] . '/assets/com-footer.tpl';)
?>

I am using a MAMP localhost to run my website and I am puzzled as to how to get this to work? I'm no pro unfortunately!! The initial intention was to get the baseURL before the /assets/com-footer.tpl The directory of my tpl files is: localhost/assets

I am trying to avoid having to put ../../assets/file.tpl for some pages. I just want a standard php code that outputs the baseURL of the localhost and the server when it is uploaded to a hosting server. When I have this, I can put it before css stylesheet links and images etc. All to avoid the ../../../ strings I have to add depending on which folder the webpage is saved.

Can anyone shed some light and help me out here?

Change

include ($_SERVER['HTTP_HOST'] . '/assets/com-footer.tpl';)

To

include ($_SERVER['HTTP_HOST'] . '/assets/com-footer.tpl');

If you know your absolute path on the server just set it yourself. I do that sometimes to shorten my PHP code. Create a variable called DOC_ROOT in a php file that is included with all your pages like a config file, header or something. something like so.

<?php
    define("DOC_ROOT","/absolute/path/to/httpdocs");
//whatever else after this
?>

Then you can just call include(DOC_ROOT.'/assets/com-footer.tpl'); and use DOC_ROOT anywhere else too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM