我包括我的库下面的代码的主入口点(main.php):
/**
* Build current url, depending on protocal (http/https),
* port, server name and path suffix
*/
$site_root = 'http';
if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
$site_root .= "s";
$site_root .= "://" . $_SERVER["SERVER_NAME"];
if ($_SERVER["SERVER_PORT"] != "80")
$site_root .= ":" . $_SERVER["SERVER_PORT"];
$site_root .= $g_config["paths"]["site_suffix"];
$g_config["paths"]["site_root"] = $site_root;
$ g_config是包含配置选项的全局数组。因此,site_suffix在开发框中可能类似于:“/ sites_working/thesite/public_html”,而在具有虚拟主机(域名)的服务器上只是“/”。
这种方法也很好,因为如果有人键入开发框的IP地址,它将使用相同的IP地址来构建javascript文件夹的路径,而不是像“localhost”之类的东西,如果您使用“localhost”它将使用“localhost”来构建URL。
而且由于它还检测SSL,所以如果您曾向服务器添加SSL支持,则不必担心天气情况,您的资源将通过HTTP或HTTPS发送。
然后,在你的模板,或者使用
<link id="site_root" href="<?php echo $g_config["paths"]["site_root"] ?>"/>
或者
<script type = "text/javascript">
var SiteRoot = "<?php echo $g_config["paths"]["site_root"]; ?>";
</script>
我想后者会更快。
当然啊。似乎我错过了最简单的答案:)。 – Staale 2008-09-20 18:14:14