Peter Stuart
Peter Stuart

Reputation: 2434

How can I add a URL to a query string?

I have a small problem with my PHP script. I want to be able to have a URL within a query string so it would look like this:

http://example.com/?url=http://google.com/

This works absolutely fine and $_GET['url'] will return http://google.com.

The problem is when the URL in my query string already has query string, for example:

http://example.com/?url=http://www.amazon.com/MP3-Music-Download/b/ref=sa_menu_mp3_str?ie=UTF8&node=163856011

will return:

http://www.amazon.com/MP3-Music-Download/b/ref=sa_menu_mp3_str?ie=UTF8

and I want it to return:

http://www.amazon.com/MP3-Music-Download/b/ref=sa_menu_mp3_str?ie=UTF8&node=163856011

I am using PHP for server side.

Could anybody please help?

Update

I am using Codeigniter, so if this is the reason why it isn't working as it should then please let me know.

Upvotes: 0

Views: 117

Answers (2)

MD SHAHIDUL ISLAM
MD SHAHIDUL ISLAM

Reputation: 14523

Use urldecode() to pass query string

Upvotes: 1

Shimon Rachlenko
Shimon Rachlenko

Reputation: 5517

You need to encode the url passed as query argument:

  • If you send it from PHP, use urlencode or rawurlencode.
  • If you send it from JS, use encodeURIComponent.

Upvotes: 3

Related Questions