4

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

0

5 Answers 5

10

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."

Sign up to request clarification or add additional context in comments.

1 Comment

That's true. Here is quote from PHP.net - The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results (source: php.net/manual/en/function.urldecode.php)
9

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

Comments

0

It is encoded into ASCII format .

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

4 Comments

@Charles php.net/urlencode does not mention about ASCII format, that's why i gave that link
Thankyou for the answer! So ASCII format, is there a PHP function to decode this or do I have to build a quick engine filled with string replacing functions? :)
It does when I just have the encoded URL as a string but for some reason when I have it in the URL bar and I use the _GET function it doesn't work. <?php print urldecode($_GET["url"]);?>
The problem is the pound sign isn't encoded into the URL and I have no access to change the encoding of the URLs that they made, so how do I get around this?
0

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.

Comments

0

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

2 Comments

This works in a string but when I have it in the url and I use the _get function it doesn't work. <?php print urldecode($_GET["url"]);?>
The problem is the pound sign isn't encoded into the URL and I have no access to change the encoding of the URLs that they made, so how do I get around this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.