PHP URL 解码 GET

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

PHP URL decode GET

phpgetdecodeurlencodeurldecode

提问by Albert Renshaw

I have been using URL decode on encoded URL variables from $_get.

我一直在对来自 $_get 的编码 URL 变量使用 URL 解码。

The current problem I am facing is I have a URL encoded like this: blah.php?url=http%3A%2F%2Fm.youtube.com%2F#/watch?feature=player_embedded&v=zd7c5tQCs1I&desktop_uri=%2Fwatch%3Fv%3Dzd7c5tQCs1I%26feature%3Dplayer_embedded

我目前面临的问题是我有一个这样编码的 URL: blah.php?url=http%3A%2F%2Fm.youtube.com%2F#/watch?feature=player_embedded&v=zd7c5tQCs1I&desktop_uri=%2Fwatch%3Fv%3Dzd7c5tQCs1I%26feature%3Dplayer_embedded

I'm not sure what kind of encoding this is, can someone help me? When I use just "urldecode" on this it just returns m.youtube.com

我不确定这是哪种编码,有人可以帮助我吗?当我只使用“urldecode”时,它只会返回m.youtube.com

Edit: My problem is not that url decode isn't working, it works if I manually enter this encoded URL and use urldecode(), but when this encoded url is in the actual pages url and I use the _GET function then I try to decode it it stripes off everything after the "#" in the URL.

编辑:我的问题不是 url 解码不起作用,如果我手动输入这个编码的 URL 并使用 urldecode(),它会起作用,但是当这个编码的 url 在实际页面 url 中并且我使用 _GET 函数时,我尝试对其进行解码,它会删除 URL 中“#”之后的所有内容。

<?php print urldecode($_GET["url"]);?>

<?php print urldecode($_GET["url"]);?>

It returns

它返回

"http://m.youtube.com/"

"http://m.youtube.com/"

instead of

代替

"http://m.youtube.com/#/watch?feature=player_embedded&v=zd7c5tQCs1I&desktop_uri=/watch?v=zd7c5tQCs1I&feature=player_embedded"

"http://m.youtube.com/#/watch?feature=player_embedded&v=zd7c5tQCs1I&desktop_uri=/watch?v=zd7c5tQCs1I&feature=player_embedded"

I think the issue is that the pound sign is not encoded, if I refresh the page it strips away the pound sing and everything after it, so how do I get around this? Can I still retrieve the info from "GET" even though there is a pound sign? (#)

我认为问题在于未编码英镑符号,如果我刷新页面,它会删除英镑符号及其后的所有内容,那么我该如何解决这个问题?即使有英镑符号,我仍然可以从“GET”中检索信息吗?(#)

采纳答案by Albert Renshaw

So here is the problem, the pound sign (#) (Hash) wasn't encoded... since I can't go back and re-encode it I have to use javascript (ex. alert(window.location.hash);) to send me the full URL after the hash then I append it to PHP's version of the URL, I THEN use a find and replace function in PHP to replace the "#" with "%23", then I use the urldecode method and it returns the full proper url decoded.

所以这里的问题是,井号 (#) (Hash) 没有被编码......因为我不能回去重新编码它我必须使用 javascript(例如 alert(window.location.hash) ;) 在散列后向我发送完整的 URL,然后我将其附加到 PHP 版本的 URL,然后我使用 PHP 中的查找和替换函数将“#”替换为“%23”,然后我使用 urldecode 方法它返回完整的正确 url 解码。

回答by T.Todua

The problem is that the full link has multiple =signs, and browser cant determine, that the other =signs refer just to the url=parameter. in your case, at first, you need to use function before link is given to url=parameter:

问题是完整链接有多个=标志,浏览器无法确定,其他=标志仅引用url=参数。在您的情况下,首先,您需要在将链接提供给url=参数之前使用函数:

========================= 1) JAVASCRIPT ======================
<script type="text/javascript">
    var mylink = encodeURIComponent('http://testest.com/link.php?name=sta&car=saab');
    document.write("http://yoursite.com/url=" + mylink);
</script>


========================= 2)or PHP ===========================
<?php
    $mylink = 'http://testest.com/link.php?name=sta&car=saab';
    echo 'http://yoursite.com/url='.urlencode($mylink);
?>

so, your output (url parameter) will get like this

所以,你的输出(url参数)会像这样

http://yoursite.com/url=http%3A%2F%2Ftest.com%2Flink.php%3Fname%3Dsta%

so, the url parameter will get the encoded url. after that, your .php file needs to decode that "url" parameter-

因此, url 参数将获得编码后的 url。之后,您的 .php 文件需要解码该“url”参数-

<?php
$varr = $_GET['url'];
$varr = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\1;",urldecode($varr)); 
$varr = html_entity_decode($varr,null,'UTF-8');
echo $varr;
?>

that will give you the correct value

这会给你正确的价值

回答by Alexandru

I read on php.net about urldecode function and they say that superglobal $_get is already decoded, ex: "The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results."

我在 php.net 上阅读了有关 urldecode 函数的内容,他们说超全局 $_get 已经解码,例如:“超全局 $_GET 和 $_REQUEST 已经解码。在 $_GET 或 $_REQUEST 中的元素上使用 urldecode() 可能会出现意外和危险的结果。”

回答by user7282

It is encoded into ASCII format .

它被编码为 ASCII 格式。

see http://www.w3schools.com/tags/ref_urlencode.asp

http://www.w3schools.com/tags/ref_urlencode.asp

回答by LiviuB

This encoding is called percent encodingor URL encoding. You can use urldecode for decoding it. (Example: http://phpfiddle.org/lite/code/0nj-198)

这种编码称为百分比编码URL 编码。您可以使用 urldecode 对其进行解码。(例如:http: //phpfiddle.org/lite/code/0nj-198