<?php
/**
* 取得當前請求的完整 URL (含查詢字串)
*/
function getCurrentUrl(): string {
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? 'localhost';
$uri = $_SERVER['REQUEST_URI'] ?? '';
return $scheme . '://' . $host . $uri;
}
/** 取得不含查詢字串的基底 URL */
function getBaseUrl(): string {
$url = getCurrentUrl();
$pos = strpos($url, '?');
return $pos !== false ? substr($url, 0, $pos) : $url;
}
/** 取得網站根目錄 URL */
function getSiteRoot(): string {
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? 'localhost';
$script = $_SERVER['SCRIPT_NAME'] ?? '';
$dir = dirname($script);
return $scheme . '://' . $host . rtrim($dir, '/') . '/';
}
?>
| 變數 | 含義 | 範例 |
|---|---|---|
REQUEST_URI | 請求 URI (含查詢字串) | /article/123?page=2 |
SCRIPT_NAME | 當前執行腳本路徑 | /index.php |
PHP_SELF | 腳本路徑 (相對 DocumentRoot) | /index.php |
QUERY_STRING | 查詢字串 (不含 ?) | page=2&sort=desc |
HTTP_HOST | Host 標頭 (含端口) | example.com:8080 |
SERVER_NAME | 伺服器設定名稱 | example.com |
HTTPS | HTTPS 時為 'on' | on |
SERVER_PORT | 伺服器端口 | 443 |
若 PHP 位於 Nginx/Cloudflare/ALB 後方,需信任 X-Forwarded-* 標頭:
<?php
function getTrustedScheme(): string {
// 信任代理標頭 (需確認代理確實設定)
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
$protos = explode(',', $_SERVER['HTTP_X_FORWARDED_PROTO']);
return trim($protos[0]) === 'https' ? 'https' : 'http';
}
return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
}
function getTrustedHost(): string {
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$hosts = explode(',', $_SERVER['HTTP_X_FORWARDED_HOST']);
return trim($hosts[0]);
}
return $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? 'localhost';
}
function getCurrentUrlTrusted(): string {
return getTrustedScheme() . '://' . getTrustedHost() . ($_SERVER['REQUEST_URI'] ?? '');
}
?>
request()->fullUrl()、url()->current()、URL::current() (自動處理代理,需設定 TrustedProxies 中介層)。$request->getUri()、$request->getSchemeAndHttpHost()。$request->getUri()->__toString()。$_SERVER['PHP_SELF'] 易受 XSS 攻擊(表單 action 直接輸出),必須 htmlspecialchars()。REQUEST_URI 在某些 CGI 模式下可能不包含查詢字串,需回退 QUERY_STRING。<?php
class UrlHelper {
public static function current(): string {
static $cached = null;
if ($cached !== null) return $cached;
$scheme = self::detectScheme();
$host = self::detectHost();
$uri = $_SERVER['REQUEST_URI'] ?? '';
return $cached = $scheme . '://' . $host . $uri;
}
private static function detectScheme(): string {
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
return (explode(',', $_SERVER['HTTP_X_FORWARDED_PROTO'])[0] === 'https') ? 'https' : 'http';
}
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
}
private static function detectHost(): string {
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
return explode(',', $_SERVER['HTTP_X_FORWARDED_HOST'])[0];
}
return $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? 'localhost';
}
public static function currentWithoutQuery(): string {
$url = self::current();
$pos = strpos($url, '?');
return $pos !== false ? substr($url, 0, $pos) : $url;
}
}
?>
正確取得當前 URL 是建構分享連結、Canonical 標籤、OAuth 回調、支付通知等功能的基礎,務必處理代理環境與安全輸出。