PHP URL decode GET

Albert Renshaw picture Albert Renshaw · Dec 10, 2012 · Viewed 38.4k times · Source

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

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

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

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.

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

It returns

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

instead of

"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? (#)

Answer

T.Todua picture T.Todua · Mar 26, 2013

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:

========================= 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

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-

<?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