Lukas
Lukas

Reputation: 1862

PHP - URL GET Variable Issues

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?

Upvotes: 2

Views: 255

Answers (3)

penartur
penartur

Reputation: 9922

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

Upvotes: 0

skyronic
skyronic

Reputation: 1635

Do this:

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

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799570

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

Upvotes: 3

Related Questions