获取";主页“;带有PHP的Url


Get "Home" Url with PHP

我想用PHP获得"主页"url不是当前URL。

我的网站存在于本地和实时测试服务器上。URL被打印为一个javascript变量,需要在两个地方工作(实时和本地)。

实时"家庭"URL为http://pipeup.pagodabox.com,本地家庭为http://192.168.0.10:8888/pipeup

因此,如果主页是http://192.168.0.10:8888/pipeup/,如果我在任何子页面上,比如http://192.168.0.10:8888/pipeup/page.phphttp://192.168.0.10:8888/pipeup/about/our-team.php,我希望有一个返回"http://192.168.0.10:8888/pipeup/""http://pipeup.pagodabox.com"的变量,这取决于我是现场查看还是本地查看。

现在我使用的是:

<?php echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI']; ?>

但当我在一个不合格的舞台上时,这是行不通的。

如何使用PHP获取主页URL?我希望该解决方案也能正确使用本地主机服务器或本地IP地址。

我在应用程序中使用了以下代码,希望它也能帮助您。

注意:如果你只是想回家,为什么不直接使用http://example.com?下面的代码为您提供当前的URL

/*
http://www.webcheatsheet.com/PHP/get_current_page_url.php
PHP: How to Get the Current Page URL
    Print   Bookmark and Share
Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your visitors submit a blog post to Digg you need to get that same exact URL. There are plenty of other reasons as well. Here is how you can do that.
Add the following code to a page:
*/
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 //return preg_replace("/'.php.*/", ".php", $pageURL);
 $_SESSION['thisurl'] = $pageURL;
 return $pageURL;
}
$directory = "/";
function url($directory = null) {
    $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != "off") ? "https" : "http";
    return $protocol . "://" . $_SERVER['HTTP_HOST'] . $directory;
}
define("BASE_URL", url($directory));

我目前在MVC设计应用程序中使用index.php中的上述内容。它允许您将目录设置为您想要的任何目录(如果在显示的第一个文件中,则主目录为"/")。然后,我只从应用程序中的任何后续文件中调用BASE_URL。不知道它是否会帮助你,但还没有辜负我!