php PHP获取带参数的URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13427177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Get URL with Parameter
提问by Cross Vander
I want to get a URL and its parameters. Example:
我想获取一个 URL 及其参数。例子:
www.someweb.com/somepage.php?id=10
How to get only somepage.php?id=10?
怎样才能得到somepage.php?id=10呢?
I'm already tried REQUEST_URIand PHP_SELFbut it only gives me www.someweb.com/somepage.phpor somepage.php. What I want to is just the page name and its parameter(s).
我已经尝试过了REQUEST_URI,PHP_SELF但它只给我www.someweb.com/somepage.php或somepage.php. 我想要的只是页面名称及其参数。
回答by Cross Vander
Finally found this method:
终于找到了这个方法:
basename($_SERVER['REQUEST_URI']);
This will return all URLs with page name. (e.g.: index.php?id=1&name=rr&class=10).
这将返回所有带有页面名称的 URL。(例如:)index.php?id=1&name=rr&class=10。
回答by miralong
for complette URL with protocol, servername and parameters:
对于带有协议、服务器名和参数的完整 URL:
$base_url = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'];
$url = $base_url . $_SERVER["REQUEST_URI"];
回答by Mohd Moe
$_SERVER['PHP_SELF']for the page name and $_GET['id']for a specific parameter.
$_SERVER['PHP_SELF']用于页面名称和$_GET['id']特定参数。
try print_r($_GET);to print out all the parameters.
尝试print_r($_GET);打印出所有参数。
for your request echo $_SERVER['PHP_SELF']."?id=".$_GET['id'];
为您的要求 echo $_SERVER['PHP_SELF']."?id=".$_GET['id'];
return all the parameters echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
返回所有参数 echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
回答by Leon Armstrong
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
echo "The current page is ".curPageName()."?".$_SERVER['QUERY_STRING'];
This will get you page name , it will get the string after the last slash
这将获得您的页面名称,它将获得最后一个斜杠后的字符串
回答by Kosta
Here's probably what you are looking for: php-get-url-query-string. You can combine it with other suggested $_SERVERparameters.
这可能是您要查找的内容:php-get-url-query-string。您可以将其与其他建议的$_SERVER参数结合使用。

