开发者

How do I extract a URL that is itself passed as URL parameter without translating the percent characters?

开发者 https://www.devze.com 2023-04-02 10:15 出处:网络
I\'m passing a URL as a URL parameter, like this: save_deal.php?url=http://my.angieslist.com/thebigdeal/default.aspx?itemid=%2bTZi%2bk0M5%2b8%3d&user=1965

I'm passing a URL as a URL parameter, like this:

save_deal.php?url=http://my.angieslist.com/thebigdeal/default.aspx?itemid=%2bTZi%2bk0M5%2b8%3d&user=1965

In save_deal.php, I access the url parameter quite simply like this:

$url = $_GET开发者_运维问答["url"]; 

But the value of $url is then

http://my.angieslist.com/thebigdeal/default.aspx?itemid= TZi k0M5 8=

That is, the weird %2b and other such percent characters have been converted into something else. How can I get the exact same string instead of this translated version (which has converted the percent characters into something else)?


$_GET will always contain the normalized and splitted values. You might be able to recover the raw parameter from the QUERY_STRING with:

$url = substr(strstr($_SERVER["QUERY_STRING"], "url="), 4);

A regex would be more reliable, and this will only work if the ?url= parameter is really present and if no other real parameters follow.


Your problem is that the URL should get encoded again when passed as a query string argument, making the itemid value encoded twice - once as an argument to angieslist, and than again as part of the entire URL (which is itself a query string argument).

To make that clearer, the code that creates the URL should look something like that:

<?php
$itemid = '+TZi+k0M5+8=';
$angieslist_url = 'http://my.angieslist.com/thebigdeal/default.aspx?itemid=' . urlencode($itemid);
// First of all, the itemid is encoded when passed as a query string argument, resulting in:
// http://my.angieslist.com/thebigdeal/default.aspx?itemid=%2bTZi%2bk0M5%2b8%3d


$savedeal_url = 'save_deal.php?url=' . urlencode($angieslist_url);    
// Here, the entire URL is used as an query string argument, which means it should be encoded just like any other argument, resulting in:
// save_deal.php?url=http%3A%2F%2Fmy.angieslist.com%2Fthebigdeal%2Fdefault.aspx%3Fitemid%3D%252BTZi%252Bk0M5%252B8%253D

Your HTTP server will take care of decoding query string arguments back to plain text, which means that when you're reading $_GET['url'] you won't get the fully encoded URL - one "layer" of encoding will be stripped off, resulting in the correct URL - http://my.angieslist.com/thebigdeal/default.aspx?itemid=%2bTZi%2bk0M5%2b8%3d.

0

精彩评论

暂无评论...
验证码 换一张
取 消