尝试,包括他们使用`DOCUMENT_ROOT为你的PHP文件,即:
include($_SERVER['DOCUMENT_ROOT']."folder/header.php");
这是假设,在浏览器中观察时,header.php中可以通过去http://127.0.0.1/folder/header.php
对于其他文件ound,如CSS,Javascript代码,你可以定义位置如下:
define("SCRIPTS_URL", "http://127.0.0.1/_scripts/");
包括上述在你的header.php文件,并确保包括header.php文件调用实际的HTML头之前,例如:只
$project = "project_x";
include($_SERVER['DOCUMENT_ROOT'].$project."/header.php");
define("SCRIPTS_URL", "http://127.0.0.1/".$project."/_scripts/");
如果你不喜欢它上面,那么你:
<?php
include($_SERVER['DOCUMENT_ROOT']."folder/header.php");
?>
<html>
<link rel="stylesheet" type="text/css" href="<?php echo SCRIPTS_URL; ?>stylesheet.css">
... etc etc ...
您可以进一步结合define
并建立目录部分,例如需要更改项目的变量,如果你看到...
更新
以下是index.php文件:
<?php
// Make the header relative to index.php (as we don't know the project) - assume header is located at /_template/header.php and this file is located at /index.php [if, in future you have /content/index.php - then the below would be ../_template/header.php, etc]
if(file_exists("_template/header.php")){
include_once("_template/header.php");
} else {
die('Fatal error - no header found');
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo BASE_URL; ?>styles/stylesheet.css">
</head>
<body>
// Content goes here
</body>
</html>
<?php
if(file_exists(ROOTPATH."_template/footer.php")){
include_once(ROOTPATH."_template/footer.php");
}
?>
而且header.php文件:
<?php
define("PROJECT_NAME", "project_x");
define("ROOTPATH", $_SERVER['DOCUMENT_ROOT'].PROJECT_NAME."/");
define("BASE_URL", "http://".$_SERVER['SERVER_NAME']."/".PROJECT_NAME."/"); // $_SERVER['SERVER_NAME'] automatically puts 'localhost' or the domain name in automatically
?>
正如你所看到的 - 一切都在这个头文件中定义,当它被包含在index.php
- 指数。php可以访问这些定义,就像定义完成后包含的任何其他文件一样(请注意,您不能覆盖定义并且不能定义两次相同的定义。)
来源
2012-02-12 10:09:08
MrJ
当我上传这BlueHost的,将127.0.0.1仍然有效? 问题是DOCUMENT_ROOT是本地主机/在我的XAMPP安装和Bluehost中的我的基础public_html文件夹。 – mehulkar 2012-02-13 00:20:53
我添加了'define(“BASE_URL”,“http:// localhost/001_Current_Projects /”。$ project。“/”);'header.php的顶部。问题是,我的其余文件调用header.php作为使用'include('header.php')'的第一行。我可以在每个php文件的开始处添加define base_url代码,但是如何在包含代码中包含BASE_URL? – mehulkar 2012-02-13 02:23:32
127.0.0.1与localhost相同 - 将这些更改为您的域名。如果您在使用模板系统时在整个脚本中调用了头文件,那么最好不要这样做,否则每次都必须进行大量更改 - 但是,您可以在需要时使用这些定义的变量(假设为'header.php'已包含在该特定页面被调用时) – MrJ 2012-02-13 07:54:47