php REQUEST_URI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8653307/
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 REQUEST_URI
提问by user1118904
I have the following php script to read the request in URL :
我有以下 php 脚本来读取 URL 中的请求:
$id = '/' != ($_SERVER['REQUEST_URI']) ?
str_replace('/?id=' ,"", $_SERVER['REQUEST_URI']) : 0;
It was used when the URL is http://www.testing.com/?id=123
当 URL 为 http://www.testing.com/?id=123
But now I wanna pass 1 more variable in url string http://www.testing.com/?id=123&othervar=123
但是现在我想在 url 字符串中再传递 1 个变量 http://www.testing.com/?id=123&othervar=123
how should I change the code above to retrieve both variable?
我应该如何更改上面的代码以检索两个变量?
回答by nand
You can either use regex, or keep on using str_replace
.
您可以使用正则表达式,也可以继续使用str_replace
.
Eg.
例如。
$url = parse_url($_SERVER['REQUEST_URI']);
if ($url != '/') {
parse_str($url['query']);
echo $id;
echo $othervar;
}
Output will be: http://www.testing.com/123/123
回答by Oldskool
回答by cyberseppo
perhaps
也许
$id = isset($_GET['id'])?$_GET['id']:null;
and
和
$other_var = isset($_GET['othervar'])?$_GET['othervar']:null;
回答by ???? ??????
You can simply use $_GET
especially if you know the othervar
's name.
If you want to be on the safe side, use if (isset ($_GET ['varname']))
to test for existence.
您可以简单地使用,$_GET
特别是如果您知道othervar
的名称。如果您想安全起见,请使用if (isset ($_GET ['varname']))
来测试是否存在。
回答by Sergio
Since vars passed through url are $_GET vars, you can use filter_input()
function:
由于通过 url 传递的变量是 $_GET 变量,您可以使用filter_input()
函数:
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
$othervar = filter_input(INPUT_GET, 'othervar', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
It would store the values of each var and sanitize/validate them too.
它将存储每个 var 的值并清理/验证它们。