這裏是一個不錯的簡潔的線我已經使用了很長時間,和它的作品非常漂亮。
$myURL = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
echo $myURL;
編輯使用此功能,我lifted from the PHP manual的一個編輯的版本,以及__FILE__
幻常量,它可以這樣做(未經測試):
function getRelativePath ($path, $compareTo) {
// Replace \ with/and remove leading/trailing slashes (for Windows)
$path = trim(str_replace('\\','/',$path),'/');
$compareTo = trim(str_replace('\\','/',$compareTo),'/');
// simple case: $compareTo is in $path
if (strpos($path, $compareTo) === 0) {
$offset = strlen($compareTo) + 1;
return substr($path, $offset);
}
$relative = array();
$pathParts = explode('/', $path);
$compareToParts = explode('/', $compareTo);
foreach($compareToParts as $index => $part) {
if (isset($pathParts[$index]) && $pathParts[$index] == $part) {
continue;
}
$relative[] = '..';
}
foreach($pathParts as $index => $part) {
if (isset($compareToParts[$index]) && $compareToParts[$index] == $part) {
continue;
}
$relative[] = $part;
}
return implode('/',$relative);
}
$myURL = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].'/'.ltrim(getRelativePath(realpath($_SERVER['PHP_SELF']),__FILE__),'/');
echo $myURL;
沒有它沒有意義,張貼代碼不起作用 – shikhar
嘿,夥計,我們無法訪問您的本地主機..請編輯您的答案,並添加適當的細節。 –
你想要得到哪個網址? –