php PHP未设置获取参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6829087/
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 unset get parameter?
提问by Ryan
function getUrlCurrently() {
$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
I'm using this function to determine the current URL of the page. I want to know if it is possible to extend this function to unset a pre-determined $_GET parameter.
我正在使用此函数来确定页面的当前 URL。我想知道是否可以扩展此函数以取消设置预先确定的 $_GET 参数。
All of my $_GET values are stored in an array. So I can access the specific values by using
我所有的 $_GET 值都存储在一个数组中。所以我可以通过使用访问特定的值
$my_array[0]
Is it expensive and not realistic to use my suggested logic to accomplish this task?
使用我建议的逻辑来完成这项任务是否昂贵且不现实?
EDIT: I only want to print the URL to use it as a link.
My url has GET parameters in it.
编辑:我只想打印 URL 以将其用作链接。
我的网址中有 GET 参数。
回答by Pascal MARTIN
Not sure what you really want to do with this, but $_GET
(and other super-globals)are not read-only :
不确定你真的想用它做什么,但是$_GET
(和其他超级全局变量)不是只读的:
- You can add values into them,
- You can overide values,
- And, of course, you can
unset()
values.
- 您可以向其中添加值,
- 您可以覆盖值,
- 而且,当然,你可以
unset()
重视。
Note, though, that modifying $_GET
is often not considered as good-practice : when one reads some code, he expects what's in $_GET
to come from the parameters in the URL -- and not from your code.
但是请注意,修改$_GET
通常不被视为良好实践:当人们阅读一些代码时,他希望内容$_GET
来自 URL 中的参数——而不是来自您的代码。
For instance, you can absolutely do something like this :
例如,你绝对可以做这样的事情:
unset($_GET['my_item']);
回答by Yoshi
Update to your function:
更新您的功能:
function getUrlCurrently($filter = array()) {
$pageURL = isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ? "https://" : "http://";
$pageURL .= $_SERVER["SERVER_NAME"];
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= ":".$_SERVER["SERVER_PORT"];
}
$pageURL .= $_SERVER["REQUEST_URI"];
if (strlen($_SERVER["QUERY_STRING"]) > 0) {
$pageURL = rtrim(substr($pageURL, 0, -strlen($_SERVER["QUERY_STRING"])), '?');
}
$query = $_GET;
foreach ($filter as $key) {
unset($query[$key]);
}
if (sizeof($query) > 0) {
$pageURL .= '?' . http_build_query($query);
}
return $pageURL;
}
// gives the url as it is
echo getUrlCurrently();
// will remove 'foo' and 'bar' from the query if existent
echo getUrlCurrently(array('foo', 'bar'));
回答by Treffynnon
To assemble a link with GET parameters in an array try:
要在数组中使用 GET 参数组装链接,请尝试:
unset($my_array['key']);
$url = getUrlCurrently() . '?' . http_build_query($my_array);
See http://www.php.net/manual/en/function.http-build-query.php
回答by hornetbzz
This has nthg to do with $_GET. You can just use the existing global data $_SERVER, or getenv, like this :
这与 $_GET 有关。您可以只使用现有的全局数据 $_SERVER 或 getenv,如下所示:
function GetCurrentUrl($debug=FALSE) {
$pageURL = (strtolower($_SERVER["HTTPS"]) == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
// DEBUG
if ($debug) {
$msg = "DEBUG MODE: current URL= ".$pageURL ;
if (function_exists('debug_msg')) {
debug_msg($msg , $debug) ;
}else {
echo $msg ;
}
}
return $pageURL;
}
EDIT: but I see where you are coming from with your $_GET statement. You mean the URI contents some parameters. You'll get them by $_SERVER['REQUEST_URI'], or as better suggested, using http_build_query
编辑:但我看到你的 $_GET 语句来自哪里。您的意思是 URI 包含一些参数。您将通过 $_SERVER['REQUEST_URI'] 或按照更好的建议使用http_build_query获取它们
EDIT2: On top of that, with regards to one point of your question, you can also add a work around to setup a "rewriting"-like function as described in this php manual interesting example.
EDIT2:最重要的是,关于您的问题的一点,您还可以添加一个变通方法来设置一个类似“重写”的函数,如本 php 手册有趣示例中所述。
回答by gnur
Wouldn't it be easier to user $_SERVER['SCRIPT_URI']
?
It returns the full url without query parameters.
使用起来不是更容易$_SERVER['SCRIPT_URI']
吗?
它返回没有查询参数的完整 url。
回答by Ben Erwin
//your query string is ?a=1&b=2&c=3
function unset_get($param){
//sets string to (array) $query_string
parse_str($_SERVER['QUERY_STRING'],$query_string);
//removes array element defined by param
unset($query_string[$param]);
//returns modified array as a string
return http_build_query($query_string);
}
print unset_get( 'b');
//returns "a=1&c=3"