16

Here is the url:

http://localhost/test.php?id=http://google.com/?var=234&key=234

And I can't get the full $_GET['id'] or $_REQUEST['d'].

<?php
print_r($_REQUEST['id']); 
//And this is the output http://google.com/?var=234
//the **&key=234** ain't show 
?>
Erenor Paz
  • 3,061
  • 4
  • 37
  • 44
spicykimchi
  • 1,151
  • 5
  • 22
  • 41

7 Answers7

35
$get_url = "http://google.com/?var=234&key=234";
$my_url = "http://localhost/test.php?id=" . urlencode($get_url);

$my_url outputs:

http://localhost/test.php?id=http%3A%2F%2Fgoogle.com%2F%3Fvar%3D234%26key%3D234

So now you can get this value using $_GET['id'] or $_REQUEST['id'] (decoded).

echo urldecode($_GET["id"]);

Output

http://google.com/?var=234&key=234

To get every GET parameter:

foreach ($_GET as $key=>$value) {
  echo "$key = " . urldecode($value) . "<br />\n";
  }

$key is GET key and $value is GET value for $key.

Or you can use alternative solution to get array of GET params

$get_parameters = array();
if (isset($_SERVER['QUERY_STRING'])) {
  $pairs = explode('&', $_SERVER['QUERY_STRING']);
  foreach($pairs as $pair) {
    $part = explode('=', $pair);
    $get_parameters[$part[0]] = sizeof($part)>1 ? urldecode($part[1]) : "";
    }
  }

$get_parameters is same as url decoded $_GET.

Wh1T3h4Ck5
  • 8,399
  • 9
  • 59
  • 79
  • How can I get all $_GET without specifying the 'id'? So that I can automatically read all $_GET request? – spicykimchi Apr 13 '11 at 07:29
  • 1
    $get_parameters = array(); if (isset($_SERVER['QUERY_STRING'])) { $pairs = explode('&', $_SERVER['QUERY_STRING']); foreach($pairs as $pair) { $part = explode('=', $pair); $get_parameters[$part[0]] = urldecode($part[1]); } } Warning ! You have to test if (count($part) > 1) before get some undefined offset. – Kikiwa Aug 29 '13 at 08:42
9

While creating url encode them with urlencode

$val=urlencode('http://google.com/?var=234&key=234')

<a href="http://localhost/test.php?id=<?php echo $val ?>">Click here</a>

and while fetching decode it wiht urldecode

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
2

I had a similar problem and ended up using parse_url and parse_str, which as long as the URL in the parameter is correctly url encoded (which it definitely should) allows you to access both all the parameters of the actual URL, as well as the parameters of the encoded URL in the query parameter, like so:

$get_url = "http://google.com/?var=234&key=234";
$my_url = "http://localhost/test.php?id=" . urlencode($get_url);

function so_5645412_url_params($url) {
    $url_comps = parse_url($url);
    $query = $url_comps['query'];

    $args = array();
    parse_str($query, $args);

    return $args;
}

$my_url_args = so_5645412_url_params($my_url); // Array ( [id] => http://google.com/?var=234&key=234 )
$get_url_args = so_5645412_url_params($my_url_args['id']); // Array ( [var] => 234, [key] => 234 )
Simon
  • 3,667
  • 1
  • 35
  • 49
2

You may have to use urlencode on the string 'http://google.com/?var=234&key=234'

Extrakun
  • 19,057
  • 21
  • 82
  • 129
1

you use bad character like ? and & and etc ...

edit it to new code

see this links

also you can use urlencode

$val=urlencode('http://google.com/?var=234&key=234')
Mohammad Efazati
  • 4,812
  • 2
  • 35
  • 50
0

The correct php way is to use parse_url()

http://php.net/manual/en/function.parse-url.php

(from php manual)

This function parses a URL and returns an associative array containing any of the various components of the URL that are present.

This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial URLs are also accepted, parse_url() tries its best to parse them correctly.

aimiliano
  • 1,105
  • 2
  • 12
  • 18
-1
if (isset($_SERVER['HTTPS'])){
    echo "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]";
}else{
    echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]";
}
andrewchan2022
  • 4,953
  • 45
  • 48