获取Url参数

时间:2016-04-08 06:08:54

标签: php

我遇到了一个简单的问题。我需要从url获取参数值。

网址如下:

http://www.example.com/index.php?someUrl=http://www.example.com/folder/index.htm#<id=18025>>newwnd=false

我需要在php中获取someUrl的值。

我尝试了什么:

$_GET['someUrl'];

我得到的输出是http://www.example.com/folder/index.htm

输出需要得到:

http://www.example.com/folder/index.htm#<id=18025>>newwnd=false

注意:我无法将#之后的网址分成新参数。

我希望我能够澄清这个问题。

编辑:根据有价值的反馈,我在发送之前编码了字符串作为URL参数。

我通过javascript函数进行编码encodeURIComponent 现在网址如下所示:

http://www.example.com/index.php?someUrl=http%3A%2F%2Fexample.com%folder%2Ftallyhelp.htm%23%253Cid%3D18025%253E%253Enewwnd%3Dfalse

通过urldecode函数在php中解码后,我得到了结果http://www.example.com/folder/index.htm#>newwnd=false 'id'从结果中删除。

1 个答案:

答案 0 :(得分:3)

您的问题是,如果您对网页的请求是:

http://www.example.com/index.php?someUrl=http://www.example.com/folder/index.htm#<id=18025>>newwnd=false

然后#之后的任何内容都是锚标记,这意味着后面的任何内容都不是$_GET的参数,它只是一个锚标记。所以你需要编码#(以及其他一些可能导致更多问题的奇怪字符)

因此,如果您设法按照以下方式向您的网页发出请求:

http://www.example.com/index.php?someUrl=http%3A%2F%2Fwww.example.com%2Ffolder%2Findex.htm%23%3Cid%3D18025%3E%3Enewwnd%3Dfalse

然后您就可以使用$_GET['someUrl'];获取所需的值,因为#将被编码(以及其他字符)

那么如何提出这样的请求呢?只需使用urlencodesomeUrl参数进行编码。

了解它的工作原理

echo urlencode('http://www.example.com/folder/index.htm#<id=18025>>newwnd=false');

所以在对链接进行编码后,指向该页面的链接就像这样

<a href="http://www.example.com/index.php?someUrl=http%3A%2F%2Fwww.example.com%2Ffolder%2Findex.htm%23%3Cid%3D18025%3E%3Enewwnd%3Dfalse">my link</a>

正如您所看到的那样#将被编码,这将阻止它弄乱请求的网址,您将能够通过$_GET['someUrl'];

获取此字符串

更新OP的新修改:

如果您使用js&#39; encodeURIComponent进行编码,则在PHP中使用rawurldecode()。见How to decode the url in php where url is encoded with encodeURIComponent(),其他答案也可能有所帮助。