2

I am passing a URL through a GET variable. When I echo the variable on the next page, it displays the URL without special characters. For example, take the URL:

http://example.com/upload?url=http://www.test.com/this%20is%20the%20image.jpg

When this code executes:

echo $_GET['url'];

the result is:

http://www.test.com/this is the image.jpg

How do I get exactly what the value of the GET variable is in the URL, and not have it converted upon retrieval?

3 Answers 3

3

You will need to parse $_SERVER['QUERY_STRING'] yourself if you want the raw values in the query string.

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

Comments

1

Do this:

echo rawurlencode($_GET['url']);

3 Comments

And what will your code output for the following path: http://example.com/upload?url=http://test.com/somepath?somevar=someval&somevar2=someval2?
Right! Can't do a urlencode for this. My solution is wrong here.
It is wrong as well as the entire approach of passing non-urlencoded values through the request string.
0

Consider urlencoding the url when constructing the upload?url=... path instead, which will add you more flexibility with urls like http://www.test.com/this%20is%20the%20image.jpg?with=some&additional=parameters.

So that for your example url it will be http://example.com/upload?url=http%3A%2F%2Fwww.test.com%2Fthis%2020is%2020the%2020image.jpg.

See http://php.net/urlencode for how to urlencode the variable.

Example:

Page with link

function showLink($url) {
    return "/upload?url=".urlencode($url);
}

"Upload" page

echo $_GET['url']

That way, you'll still be able to process the link resulted from showLink("http://test.com/somepath?somevar=someval&somevar2=someval2") properly. Otherwise, if you won't escape it in showLink, you'll end up with the url like http://example.com/upload?url=http://test.com/somepath?somevar=someval&somevar2=someval2, and it will be quite difficult to tell whether somevar2 is the part of the request to the example.com or to the test.com (btw, $_GET['url'] will contain http://test.com/somepath?somevar=someval in such a case).

3 Comments

This is for an API, so I would prefer links just be simply pasted to keep things simpler.
My answer is exactly about keeping things simpler. The $_GET variable will contain exactly the urls you want, without the need to someway parsing it from the request-string. BTW you shouldn't create the request string manually in the first place (compiling it from the array instead), but unfortunately PHP doesn't provide built-in tools for doing this unfortunately.
And, by the way, my answer gives you exactly the same result you'll get with <form action="http://example.com/upload" method="GET"><input type="hidden" name="url" value="http://test.com/somepath?somevar=someval&somevar2=someval2"></form>. This is just the way of storing variables in a GET request. You're instead storing the variables in some other non-standart, incomplete and incompatible way, and then wanting to get your original values after the parsing in a standart way has been done. Instead of using the standart encoding, you're inventing your own and then overcoming its drawbacks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.