php PHP中的GET URL参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5884807/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:47:29  来源:igfitidea点击:

GET URL parameter in PHP

phpurlredirect

提问by Feras Odeh

I'm trying to pass a URL as a url parameter in php but when I try to get this parameter I get nothing

我试图在 php 中将 URL 作为 url 参数传递,但是当我尝试获取此参数时,我什么也没得到

I'm using the following url form:

我正在使用以下网址形式:

http://localhost/dispatch.php?link=www.google.com

I'm trying to get it through:

我正在努力解决它:

$_GET['link'];

But nothing returned. What is the problem?

但什么都没有回来。问题是什么?

回答by álvaro González

$_GETis not a function or language construct—it's just a variable (an array). Try:

$_GET不是函数或语言结构——它只是一个变量(一个数组)。尝试:

<?php
echo $_GET['link'];

In particular, it's a superglobal: a built-in variable that's populated by PHP and is available in all scopes (you can use it from inside a function without the globalkeyword).

特别是,它是一个superglobal:一个由 PHP 填充并在所有范围内可用的内置变量(您可以在没有global关键字的函数内部使用它)。

Since the variable might not exist, you could (and should) ensure your code does not trigger notices with:

由于该变量可能不存在,您可以(并且应该)确保您的代码不会触发通知:

<?php
if (isset($_GET['link'])) {
    echo $_GET['link'];
} else {
    // Fallback behaviour goes here
}

Alternatively, if you want to skip manual index checks and maybe add further validations you can use the filterextension:

或者,如果您想跳过手动索引检查并添加进一步的验证,您可以使用过滤器扩展:

<?php
echo filter_input(INPUT_GET, 'link', FILTER_SANITIZE_URL);

Last but not least, you can use the null coalescing operator(available since PHP/7.0) to handle missing parameters:

最后但并非最不重要的是,您可以使用空合并运算符(自PHP/7.0起可用)来处理丢失的参数:

echo $_GET['link'] ?? 'Fallback value';

回答by MarcoS

Please post your code,

请贴出你的代码,

<?php
    echo $_GET['link'];
?>

or

或者

<?php
    echo $_REQUEST['link'];
?>

dowork...

工作......

回答by patrick

To make sure you're always on the safe side, without getting all kinds of unwanted code insertion use FILTERS:

为了确保您始终处于安全状态,而不会插入各种不需要的代码,请使用过滤器:

echo filter_input(INPUT_GET,"link",FILTER_SANITIZE_STRING);

More reading on php.net function filter_input, or check out the description of the different filters

更多关于php.net 函数 filter_input 的阅读,或查看不同过滤器的描述

回答by Muhammad Ashikuzzaman

Use this:

用这个:

$parameter = $_SERVER['QUERY_STRING'];
echo $parameter;

Or just use:

或者只是使用:

$parameter = $_GET['link'];
echo $parameter ;

回答by phil

The accepted answer is good. But if you have a scenario like this:

接受的答案很好。但如果你有这样的场景:

http://www.mydomain.me/index.php?state=California.php#Berkeley

You can treat the named anchor as a query string like this:

您可以将命名锚点视为查询字符串,如下所示:

http://www.mydomain.me/index.php?state=California.php&city=Berkeley

Then, access it like this:

然后,像这样访问它:

$Url = $_GET['state']."#".$_GET['city'];

回答by Saurabh Chandra Patel

     $Query_String  = explode("&", explode("?", $_SERVER['REQUEST_URI'])[1] );
    var_dump($Query_String)

Array ( [ 0] => link=www.google.com )

数组 ( [ 0] => link=www.google.com )

回答by user235510

I was getting nothing for any $_GET["..."](e.g print_r($_GET)gave an empty array) yet $_SERVER['REQUEST_URI']showed stuff should be there. In the end it turned out that I was only getting to the web page because my .htaccess was redirecting it there (my 404 handler was the same .php file, and I had made a typo in the browser when testing).

我没有得到任何东西$_GET["..."](例如print_r($_GET)给了一个空数组),但$_SERVER['REQUEST_URI']显示的东西应该在那里。最后我发现我只能进入网页,因为我的 .htaccess 将它重定向到那里(我的 404 处理程序是同一个 .php 文件,我在测试时在浏览器中打错了字)。

Simply changing the name meant the same php code worked once the 404 redirection wasn't kicking in!

简单地更改名称意味着一旦 404 重定向没有启动,相同的 php 代码就可以工作!

So there are ways$_GETcan return nothing even though the php code may be correct.

因此$_GET即使 php 代码可能正确,也有一些方法可以不返回任何内容。

回答by balazon

Whomever gets nothing back, I think he just has to enclose the result in html tags,

无论谁一无所获,我认为他只需要将结果包含在 html 标签中,

Like this:

像这样:

<html>
<head></head>
<body>
<?php
echo $_GET['link'];
?>
<body>
</html>

回答by Omer Hijazi

As Alvaro said, $_GET is not a function but an array containing the parameters So you can retrieve one element from that array using

正如 Alvaro 所说,$_GET 不是一个函数,而是一个包含参数的数组,因此您可以使用从该数组中检索一个元素

<?php
$link = $_GET['link'];
echo $link;
?>

Expected OP:

预期操作:

www.google.com