1

What is the proper protocol for storing other websites URL's in your own php page's URL?

For example if I wanted to store google it would be easy.

MySite.com/test.php?url=http://www.google.com


But what about a URL more complex than http://www.google.com, like if I wanted to store another website's PHP page.

(e.g. http://www.AnotherSite.com/page1.php?key=value&key2=value2&key3=value3)

Then how would I store it in my PHP page's URL? Do you replace all of the "&" symbols with a "\&" phrase? Is there a better method? Does that even work in the first place?


Thanks!!!

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195

4 Answers4

2

Just encode the URLs. You can use the PHP functions urlencode() and urldecode() for that.

Carsten
  • 17,991
  • 4
  • 48
  • 53
2

If you don't want to think about it:

$url = 'mysite.com/test.php?' . http_build_query(array(
    'url' => $anything_you_want,
));

Otherwise:

$url = 'mysite.com/test.php?url=' . urlencode($anything_you_want);

You could also use rawurlencode(). Notable difference is that it follows RFC 3986.

Further reading:

http_build_query() urlencode() rawurlencode()

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
2

Just encode it with urlencode() if you want to use the entire URL as one parameter:

http://us3.php.net/manual/en/function.urlencode.php

Example:

$url = 'http://www.AnotherSite.com/page1.php?key=value&key2=value2&key3=value';
echo 'http://MySite.com/test.php?url='.urlencode($url);
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
2

You can use encoding for the urls. There are very simple methods built in to PHP to do that.

http://php.net/manual/en/function.urlencode.php

Benjamin Powers
  • 3,186
  • 2
  • 18
  • 23