阿杰 ✅ 我给你整理一下 PHP 获取当前 URL 的方法,以及相关的 服务器变量说明。
一、PHP 获取当前 URL 的常见方法
1. 获取完整 URL(包含协议、域名、路径和查询参数)
function getCurrentUrl() {
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http";
$host = $_SERVER['HTTP_HOST']; // 含域名和端口
$uri = $_SERVER['REQUEST_URI']; // 含路径和参数
return $scheme . "://" . $host . $uri;
}
echo getCurrentUrl();
👉 输出示例:
http://example.com/test/index.php?id=123&name=abc
2. 获取当前脚本路径
echo $_SERVER['SCRIPT_NAME'];
👉 输出:
/test/index.php
3. 获取当前目录路径(不含文件名)
echo dirname($_SERVER['SCRIPT_NAME']);
👉 输出:
/test
4. 获取查询参数部分(? 后面的内容)
echo $_SERVER['QUERY_STRING'];
👉 输出:
id=123&name=abc
5. 获取 URI(包含路径和参数,不含域名)
echo $_SERVER['REQUEST_URI'];
👉 输出:
/test/index.php?id=123&name=abc
二、常见 $_SERVER
变量说明
变量名 | 说明 |
---|---|
$_SERVER['QUERY_STRING'] | URL 中的查询参数部分(不含 ? )。例:id=123&name=abc |
$_SERVER['REQUEST_URI'] | 请求的 URI(包含路径和查询参数)。例:/test/index.php?id=123 |
$_SERVER['SCRIPT_NAME'] | 当前执行脚本的路径(相对网站根目录)。例:/test/index.php |
$_SERVER['PHP_SELF'] | 当前执行脚本的相对路径,和 SCRIPT_NAME 类似,但在某些情况下会被 URL 重写影响。 |
$_SERVER['SCRIPT_FILENAME'] | 当前执行脚本的绝对路径。例:/var/www/html/test/index.php |
$_SERVER['DOCUMENT_ROOT'] | 网站根目录的绝对路径。例:/var/www/html |
$_SERVER['HTTP_HOST'] | 请求的 Host(域名 + 端口)。例:example.com:8080 |
$_SERVER['SERVER_NAME'] | 服务器配置的主机名(通常是域名)。例:example.com |
$_SERVER['SERVER_PORT'] | 服务器端口。默认 80 (http)、443 (https)。 |
$_SERVER['HTTPS'] | 是否使用 HTTPS(on /空)。 |
三、组合应用示例
如果你只想获取 当前页面的 URL 不含参数:
$url = strtok($_SERVER["REQUEST_URI"], '?');
echo $url;
👉 输出:
/test/index.php
如果只想获取 域名部分:
echo $_SERVER['HTTP_HOST'];
👉 输出:
example.com
好的阿杰 ✅ 我帮你写一个 通用函数 getCurrentUrl($type)
,通过传入不同的参数,可以获取完整 URL、路径、目录或查询参数。
通用函数示例
<?php
/**
* 获取当前 URL 或路径信息
*
* @param string $type 可选:
* - full 完整 URL(协议+域名+路径+参数)
* - domain 域名(含端口)
* - path 路径(含文件名,不含域名和参数)
* - dir 目录路径(不含文件名)
* - file 当前执行脚本文件名
* - query 查询参数(? 后部分)
* - noquery URL(不带查询参数)
*
* @return string
*/
function getCurrentUrl($type = 'full') {
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http";
$host = $_SERVER['HTTP_HOST']; // 域名+端口
$uri = $_SERVER['REQUEST_URI']; // 含路径和参数
$path = parse_url($uri, PHP_URL_PATH);
$query = $_SERVER['QUERY_STRING'] ?? '';
$file = basename($_SERVER['SCRIPT_NAME']);
$dir = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
switch ($type) {
case 'full':
return $scheme . "://" . $host . $uri;
case 'domain':
return $host;
case 'path':
return $path;
case 'dir':
return $dir;
case 'file':
return $file;
case 'query':
return $query;
case 'noquery':
return $scheme . "://" . $host . $path;
default:
return $scheme . "://" . $host . $uri;
}
}
使用示例
假设当前请求 URL 为:
http://example.com/test/index.php?id=123&name=abc
调用结果:
echo getCurrentUrl('full'); // http://example.com/test/index.php?id=123&name=abc
echo getCurrentUrl('domain'); // example.com
echo getCurrentUrl('path'); // /test/index.php
echo getCurrentUrl('dir'); // /test
echo getCurrentUrl('file'); // index.php
echo getCurrentUrl('query'); // id=123&name=abc
echo getCurrentUrl('noquery'); // http://example.com/test/index.php
好嘞阿杰 ✅
那我给你写一个 数组模式的函数,一次性把 完整 URL、域名、路径、目录、文件名、查询参数等信息 全部返回,使用时更方便。
一次性返回所有 URL 信息
<?php
/**
* 获取当前 URL 的完整信息
*
* @return array
*/
function getUrlInfo() {
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http";
$host = $_SERVER['HTTP_HOST']; // 域名+端口
$uri = $_SERVER['REQUEST_URI']; // 含路径和参数
$path = parse_url($uri, PHP_URL_PATH);
$query = $_SERVER['QUERY_STRING'] ?? '';
$file = basename($_SERVER['SCRIPT_NAME']);
$dir = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
return [
'scheme' => $scheme, // http / https
'host' => $host, // example.com:8080
'domain' => $_SERVER['SERVER_NAME'], // 纯域名 example.com
'port' => $_SERVER['SERVER_PORT'], // 端口号
'full' => $scheme . "://" . $host . $uri, // 完整 URL
'noquery' => $scheme . "://" . $host . $path,// 不带参数的 URL
'uri' => $uri, // /test/index.php?id=123
'path' => $path, // /test/index.php
'dir' => $dir, // /test
'file' => $file, // index.php
'query' => $query, // id=123&name=abc
'script' => $_SERVER['SCRIPT_FILENAME'], // 绝对路径 /var/www/html/test/index.php
'doc_root' => $_SERVER['DOCUMENT_ROOT'], // 网站根目录 /var/www/html
];
}
使用示例
假设当前请求是:
http://example.com:8080/test/index.php?id=123&name=abc
调用:
$info = getUrlInfo();
print_r($info);
输出:
Array
(
[scheme] => http
[host] => example.com:8080
[domain] => example.com
[port] => 8080
[full] => http://example.com:8080/test/index.php?id=123&name=abc
[noquery] => http://example.com:8080/test/index.php
[uri] => /test/index.php?id=123&name=abc
[path] => /test/index.php
[dir] => /test
[file] => index.php
[query] => id=123&name=abc
[script] => /var/www/html/test/index.php
[doc_root] => /var/www/html
)
这样你就可以直接用:
echo $info['full']; // 完整URL
echo $info['dir']; // 目录
echo $info['query']; // 查询参数
好嘞阿杰 ✅
我给你把 query 参数解析成数组 一起返回,这样你直接 $info['query_array']['id']
就能拿到参数值。
改进版函数(带 query 数组)
<?php
/**
* 获取当前 URL 的完整信息(带 query 数组)
*
* @return array
*/
function getUrlInfo() {
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http";
$host = $_SERVER['HTTP_HOST']; // 域名+端口
$uri = $_SERVER['REQUEST_URI']; // 含路径和参数
$path = parse_url($uri, PHP_URL_PATH);
$query = $_SERVER['QUERY_STRING'] ?? '';
$file = basename($_SERVER['SCRIPT_NAME']);
$dir = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
// 解析 query 参数为数组
$queryArray = [];
if (!empty($query)) {
parse_str($query, $queryArray);
}
return [
'scheme' => $scheme, // http / https
'host' => $host, // example.com:8080
'domain' => $_SERVER['SERVER_NAME'], // 纯域名 example.com
'port' => $_SERVER['SERVER_PORT'], // 端口号
'full' => $scheme . "://" . $host . $uri, // 完整 URL
'noquery' => $scheme . "://" . $host . $path,// 不带参数的 URL
'uri' => $uri, // /test/index.php?id=123
'path' => $path, // /test/index.php
'dir' => $dir, // /test
'file' => $file, // index.php
'query' => $query, // 原始 query 字符串
'query_array' => $queryArray, // 解析后的 query 数组
'script' => $_SERVER['SCRIPT_FILENAME'], // 绝对路径 /var/www/html/test/index.php
'doc_root' => $_SERVER['DOCUMENT_ROOT'], // 网站根目录 /var/www/html
];
}
使用示例
假设请求 URL 为:
http://example.com/test/index.php?id=123&name=abc&debug=true
调用:
$info = getUrlInfo();
echo $info['full']; // http://example.com/test/index.php?id=123&name=abc&debug=true
echo $info['query']; // id=123&name=abc&debug=true
print_r($info['query_array']);
输出:
Array
(
[id] => 123
[name] => abc
[debug] => true
)
这样你就可以直接:
echo $info['query_array']['id']; // 123
echo $info['query_array']['name']; // abc
好嘞阿杰 ✅
我来给你加一个 专门取单个 Query 参数的函数,支持默认值。这样就不用每次 isset()
判断了,直接调用即可。
改进版:增加 getQueryParam($key, $default = null)
<?php
/**
* 获取当前 URL 的完整信息(带 query 数组)
*
* @return array
*/
function getUrlInfo() {
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http";
$host = $_SERVER['HTTP_HOST']; // 域名+端口
$uri = $_SERVER['REQUEST_URI']; // 含路径和参数
$path = parse_url($uri, PHP_URL_PATH);
$query = $_SERVER['QUERY_STRING'] ?? '';
$file = basename($_SERVER['SCRIPT_NAME']);
$dir = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
// 解析 query 参数为数组
$queryArray = [];
if (!empty($query)) {
parse_str($query, $queryArray);
}
return [
'scheme' => $scheme, // http / https
'host' => $host, // example.com:8080
'domain' => $_SERVER['SERVER_NAME'], // 纯域名 example.com
'port' => $_SERVER['SERVER_PORT'], // 端口号
'full' => $scheme . "://" . $host . $uri, // 完整 URL
'noquery' => $scheme . "://" . $host . $path,// 不带参数的 URL
'uri' => $uri, // /test/index.php?id=123
'path' => $path, // /test/index.php
'dir' => $dir, // /test
'file' => $file, // index.php
'query' => $query, // 原始 query 字符串
'query_array' => $queryArray, // 解析后的 query 数组
'script' => $_SERVER['SCRIPT_FILENAME'], // 绝对路径 /var/www/html/test/index.php
'doc_root' => $_SERVER['DOCUMENT_ROOT'], // 网站根目录 /var/www/html
];
}
/**
* 获取单个 query 参数,带默认值
*
* @param string $key 参数名
* @param mixed $default 默认值
* @return mixed
*/
function getQueryParam($key, $default = null) {
$info = getUrlInfo();
return $info['query_array'][$key] ?? $default;
}
使用示例
假设 URL:
http://example.com/test/index.php?id=123&name=abc
调用:
echo getQueryParam('id'); // 123
echo getQueryParam('name'); // abc
echo getQueryParam('debug', 0); // 0(因为不存在 debug 参数,返回默认值)
这样你就有了两套功能:
getUrlInfo()
→ 一次性拿所有 URL 信息getQueryParam()
→ 单独取某个参数,支持默认值
发表回复