php 使用 URL 参数获取当前文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16528656/
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
Get current filename with URL parameters
提问by Mooseman
Say the user is loading the following page: www.example.com/myDirectory/myPage.php?answer=yes
And I want to use PHP to get myPage.php?answer=yes
from that. However, basename($_SERVER['PHP_SELF'])
only returns myPage.php
. How would I do that?
假设用户正在加载以下页面:www.example.com/myDirectory/myPage.php?answer=yes
我想使用 PHP 从中获取myPage.php?answer=yes
。但是,basename($_SERVER['PHP_SELF'])
只返回myPage.php
. 我该怎么做?
回答by Blazemonger
You can append the query string after you getit as an environment variable:
$myurl = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'];
If you need to consider the possibility of no query string, add a conditional test:
如果需要考虑没有查询字符串的可能性,添加条件测试:
$myurl = strlen($_SERVER['QUERY_STRING']) ? basename($_SERVER['PHP_SELF'])."?".$_SERVER['QUERY_STRING'] : basename($_SERVER['PHP_SELF']);
回答by G?kalp Turan
it is easier?:
它更容易吗?:
basename($_SERVER['REQUEST_URI'])
回答by Shiv Singh
For URLs with trailing parameters the following will place the first part of the current URL (i.e. the part before ?) within the variable $current_url[0] and then print it out.
对于带有尾随参数的 URL,以下内容会将当前 URL 的第一部分(即 ? 之前的部分)放在变量 $current_url[0] 中,然后将其打印出来。
<?
$current_url = explode("?", $_SERVER['REQUEST_URI']);
echo $current_url[0] ;
?>
回答by draxxxeus
$_SERVER['QUERY_STRING']
will give you the paramaters
会给你参数