简体   繁体   English

php include-我的代码出了什么问题?

[英]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. 我正在使用html和php代码的*.tpl文件include在我网站上的各种网页中,以填充公共空间,例如页眉和页脚。

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? 我正在使用MAMP本地主机来运行我的网站,但我对如何使其正常运行感到困惑。 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 最初的意图是在/assets/com-footer.tpl之前获取baseURL 。我的tpl文件的目录是: localhost/assets

I am trying to avoid having to put ../../assets/file.tpl for some pages. 我试图避免在某些页面上放置../../assets/file.tpl 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. 我只想要一个标准的php代码,当它上传到托管服务器时,该代码将输出localhost和服务器的baseURL。 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. 当我拥有此文件时,可以将其放在CSS样式表链接和图像等之前。所有这些都可以避免添加../../../字符串,具体取决于保存网页的文件夹。

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. 我有时这样做是为了缩短我的PHP代码。 Create a variable called DOC_ROOT in a php file that is included with all your pages like a config file, header or something. 在您的所有页面都包含的php文件中创建一个名为DOC_ROOT的变量,例如配置文件,标头或其他内容。 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'); 然后您可以调用include(DOC_ROOT.'/assets/com-footer.tpl'); and use DOC_ROOT anywhere else too. 并在其他任何地方也使用DOC_ROOT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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