PHP Conference Japan 2024

apache_lookup_uri

(PHP 4, PHP 5, PHP 7, PHP 8)

apache_lookup_uri對指定的 URI 執行部分請求並返回關於它的所有資訊

說明

apache_lookup_uri(字串 $filename): 物件|false

這會對 URI 執行部分請求。它只進行到足以獲取有關給定資源的所有重要資訊。

當 PHP 作為 Apache 模組網路伺服器安裝時,支援此函式。

參數

檔名

被請求的檔案名稱(URI)。

回傳值

一個包含相關 URI 資訊的物件。這個物件的屬性如下:

  • status
  • the_request
  • status_line
  • method
  • content_type
  • handler
  • uri
  • 檔名
  • path_info
  • args
  • boundary
  • no_cache
  • no_local_copy
  • allowed
  • send_bodyct
  • bytes_sent
  • byterange
  • clength
  • unparsed_uri
  • mtime
  • request_time

失敗時回傳 false

範例

範例 #1 apache_lookup_uri() 範例

<?php
$info
= apache_lookup_uri('index.php?var=value');
print_r($info);

if (
file_exists($info->filename)) {
echo
'file exists!';
}
?>

上述範例會輸出類似以下的內容:

stdClass Object
(
    [status] => 200
    [the_request] => GET /dir/file.php HTTP/1.1
    [method] => GET
    [mtime] => 0
    [clength] => 0
    [chunked] => 0
    [content_type] => application/x-httpd-php
    [no_cache] => 0
    [no_local_copy] => 1
    [unparsed_uri] => /dir/index.php?var=value
    [uri] => /dir/index.php
    [filename] => /home/htdocs/dir/index.php
    [args] => var=value
    [allowed] => 0
    [sent_bodyct] => 0
    [bytes_sent] => 0
    [request_time] => 1074282764
)
file exists!

新增註解

使用者貢獻的註解 3 則註解

2
tester
16 年前
錯誤:在 Apache 2 中,apache_lookup_uri("/directory") 會顯示警告且無法回傳任何值。 apache_lookup_uri("/directory/") 可以正常運作。

另一個錯誤:virtual("something") 會強制清空標頭。我知道這有記錄在文件中,但如果它不要這樣做會更好。當您使用 virtual() 來包含動態檔案(例如 PHP 或 Perl 檔案)時,您永遠不會想要清空標頭,這表示 virtual() 無法用於大多數網站的內容 :-(。
1
niels dot kootstra at gmail dot com
16 年前
這是一個非常有用的函式,但它不會顯示所有輸出。例如,我只看到
[status]
[the_request]
[method]
[mtime]
[clength]
[chunked]
[content_type]
[no_cache]
[no_local_copy]
[unparsed_uri]
[uri]
[filename]
[path_info]
[allowed]
[sent_bodyct]
[bytes_sent]
[request_time]
0
redbeard at mdjohnson dot nospam dot us
21 年前
一個有用的特性是,如果您啟用了內容協商 (Options MultiViews),Apache 會盡可能為您解決協商。因此,www.example.com/blah 將會根據情況解析為 /base/blah.php 或 /base/blah.html 甚至 /base/blah.en.html。
To Top