James
James

Reputation: 666

Are there any tricks to avoid having to urlencode a url in a querystring

I'm trying to put a script together for a client that needs to be able to accept a web address in a query string without it first being urlencoded. An example would be like this:

http://foo.com/script.php?url=www.amazon.co.uk/ESET-Smart-Security-User-Year/dp/B005NPFOBM/ref=sr_1_1?s=software&ie=UTF8&qid=1341685530&sr=1-1

However, when I echo out the contents of $_GET['url'] it gives me the following:

www.amazon.co.uk/ESET-Smart-Security-User-Year/dp/B005NPFOBM/ref=sr_1_1?s=software

So basically it seems to choke on the first ampersand - i'm guessing because it thinks that its another variable.

Aside form urlencoding, are there any tricks to getting this working? I could probably POST it from a form, but this defeats the idea of the script.

Upvotes: 0

Views: 336

Answers (2)

Julian Knight
Julian Knight

Reputation: 4923

For this specific use case, you should use $_SERVER['QUERY_STRING'] instead. This will give you the full query string in one go, you can then split it yourself.

In your example, PHP is assuming that the & is the delimiter for the next GET variable.

Upvotes: 1

user1494736
user1494736

Reputation: 2400

you could ask the query parameters, and add them to the URL you received. List the remaining parameters in $_GET in the proper order, and add them add the end of $_GET['url'].

$_GET['url']
 + '&ie=' + $_GET['ie']
 + '&qid=' + $_GET['qid']
 + '&sr=' + $_GET['sr']

Be careful that you might get an extra parameter url someday. http://foo.com/script.php?url=www.amazon.co.uk/ESET-Smart-Security-User-Year/dp/B005NPFOBM/ref=sr_1_1?s=software&ie=UTF8&qid=1341685530&sr=1-1&url=http://someAmazoneStuff

Upvotes: 0

Related Questions