2011-09-05 77 views
0

我有一個名爲index.php文件,其中包括從另一個文件(class/event.class.php)用下面的代碼的類:如何在PHP中獲取當前包含腳本的url?

<?php 
     require_once('class/event.class.php'); 
     $evt = new Event; 
     $evt->doSomething(); 
?> 

文件event.class.php應當能夠讀在請求中使用的URL($_SERVER["SCRIPT_FILENAME")到文件

即:http://localhost:8888/class/event.class.php

我如何做到這一點

+2

沒有它沒有意義,張貼代碼不起作用 – shikhar

+0

嘿,夥計,我們無法訪問您的本地主機..請編輯您的答案,並添加適當的細節。 –

+0

你想要得到哪個網址? –

回答

2

使用$_SERVER陣列,例如:

$protocol = 'http'; 
$port = ''; 

if (isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'on') == 0) { 
    $protocol = 'https'; 
} 
if (isset($_SERVER['SERVER_PORT'])) { 
    $port = ':' . $_SERVER['SERVER_PORT']; 
} 

// Don't display the standard ports :80 for http and :443 for https 
if (($protocol == 'http' && $port == ':80') || ($protocol == 'https' && $port == ':443')) { 
    $port = ''; 
} 

$host_url = $protocol . '://' . $_SERVER['SERVER_NAME'] . $port; 
$base_url = rtrim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']), '/'); 
+0

很多錯誤: 解析錯誤:語法錯誤,意想不到 ')' 在C:\瓦帕\ wwwT2 \上線類\ timeline.class.php 53 解析錯誤:語法錯誤在C,意外 ')': \ wamp \ wwwT2 \ class \ timeline.class.php 54行 警告:basename()期望至多2個參數,3在C:\ wamp \ wwwT2 \ class \ timeline.class.php中給出54行 警告:str_replace()需要至少3個參數,2給出在第54行C:\ wamp \ wwwT2 \ class \ timeline.class.php –

+0

對不起,從系統複製粘貼它,所以不得不稍微修改它。我會手動輸入語法錯誤 - 再試一次。 – Rijk

1

這裏是一個不錯的簡潔的線我已經使用了很長時間,和它的作品非常漂亮。

$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; 
+0

這顯示了index.php文件的位置。我需要URL到events.class.php文件,所以這並不能真正幫助我,因爲我不能假設events.class.php在文件夾「class」 –

+0

內請參閱我的編輯.... – DaveRandom

+0

He必須獲取文件的URL。而URL不包括'..',還是他們? –

1

我做些什麼來讓瀏覽器路徑到我的文件(我包含在index.php中,但在event.class.php中有此代碼將返回文件夾):

$this_file = dirname($_SERVER['PHP_SELF']) . '/'; 

$a_file_that_you_want_to_access_by_url = $this_file.'class/event.class.php'; 

另外,如果你要訪問的當前目錄「根」在所有的.php文件,定義一個常數是這樣的:

define('uri_root',   dirname($_SERVER['PHP_SELF']) . '/', true); 
$a_file_that_you_want_to_access_by_url = uri_root.'class/event.class.php'; 

該腳本將與所有子文件夾呼應腳本名

echo dirname($_SERVER['PHP_SELF']).$_SERVER['PHP_SELF'];