2013-07-25 47 views
0

我想用PHP獲得了「家」的網址。 不是當前的URL。找「家」的網址與PHP

我的網站在本地和現場測試服務器上存在。該URL正在作爲javascript變量打印,需要在兩個地方(即時和本地)中工作。

活「家」的網址是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的主頁網址是什麼?我希望該解決方案也可以正常使用本地主機服務器或本地IP地址。

+1

print_r($ _ SERVER); //你會找到一個符合該法案的地方。 ^^ – JimL

+0

你爲什麼包括'$ _SERVER ['REQUEST_URI']'?這不是必需的 – NotMe

+0

你可以隨時做好舊式的實際鏈接到你的網站。 http://www.example.com。 – 2013-07-26 00:03:44

回答

1

我用下面的代碼在我的申請,我希望它幫你還。

注意:如果你只是想回家,爲什麼不使用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; 
} 
+0

我的頁面本地存在,並且在實時測試服務器上。該URL正在作爲JavaScript變量打印,需要在兩個地方都可以使用。 –

+0

該代碼不適用於本地IP,請參閱修訂後的問題。謝謝! –

1
$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)); 

我目前使用上述從我的index.php在MVC設計中的應用。它允許您將目錄設置爲任何你想要的(如果在第一個文件中出現了你的主目錄,則爲「/」)。然後,我只需從應用程序中的任何後續文件調用BASE_URL。不知道它是否會幫助你,但還沒有讓我失望!