Vadiklk
Vadiklk

Reputation: 3764

$_GET doesn't work when one of the parameters is a URL

$_GET does not work when one of the parameters to the page is a url.

An external page (which I do not have control on) shows an iframe to my page and it passes parameters of which one is:

turkSubmitTo=http%3A%2F%2Fwww.mturk.com

When on my page I want to extract other parameters, it gives me NULL for everything, but when I remove the "http" it works. Why is that and what can I do to get the other parameters?

EDIT: You can try it yourself here:

http://www.translate.outofscopes.com/?turkSubmitTo=http%3A%2F%2Fwww.mturk.com

The Array() down there is a print_r of $_GET, you can try to remove the 'http' in the parameter and it will work. On the localhost it works perfectly.

Upvotes: 0

Views: 2083

Answers (2)

Tim G
Tim G

Reputation: 1822

You can do this, pretty easily, but you need to control what's creating the url. The trick is to urlencode twice

<?

if ( array_key_exists( 'url', $_GET ) )
{
    echo $_GET['url'] . '<br>';
    echo urldecode( $_GET['url'] ) . '<br>';
}

$url = 'http://example.com/a/index.php?a=123';
$encUrl = urlencode( urlencode( $url ) );

?>
<a href="http://<?= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?url=' . $url; ?>">Not the best</a> - I've seen this fail.
<br>
<a href="http://<?= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?url=' . $encUrl; ?>">Much Better</a>

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Try Something like:

$parameters = array();
if (isset($_SERVER['QUERY_STRING'])) {
  $pairs = explode('&', $_SERVER['QUERY_STRING']);
  foreach($pairs as $pair) {
    $part = explode('=', $pair);
    $parameters[$part[0]] = urldecode($part[1]);
    }
  }

Upvotes: 3

Related Questions