231

I'm trying to pass a URL as a url parameter in php but when I try to get this parameter I get nothing

I'm using the following url form:

http://localhost/dispatch.php?link=www.google.com

I'm trying to get it through:

$_GET['link'];

But nothing returned. What is the problem?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Feras Odeh
  • 9,136
  • 20
  • 77
  • 121

7 Answers7

349

$_GET is not a function or language construct—it's just a variable (an array). Try:

<?php
echo $_GET['link'];

In particular, it's a superglobal: a built-in variable that's populated by PHP and is available in all scopes (you can use it from inside a function without the global keyword).

Since the variable might not exist, you could (and should) ensure your code does not trigger notices with:

<?php
if (isset($_GET['link'])) {
    echo $_GET['link'];
} else {
    // Fallback behaviour goes here
}

Alternatively, if you want to skip manual index checks and maybe add further validations you can use the filter extension:

<?php
echo filter_input(INPUT_GET, 'link', FILTER_SANITIZE_URL);

Last but not least, you can use the null coalescing operator (available since PHP/7.0) to handle missing parameters:

echo $_GET['link'] ?? 'Fallback value';
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 15
    If you got nothing with this, like me, than you should know, that 'link' is a name of variable in URL address! So with $_GET['link']; you need to enter URL like this: http://localhost/?link=test – Firzen Apr 20 '14 at 12:58
  • I'm using WAMP on `localhost` and this solution it's not working. `http://localhost/station?type=2` `echo $_GET['type'];` is `null` – NineCattoRules May 29 '19 at 21:58
  • 1
    Finally I used `parse_str(parse_url($actual_link)['query'], $params);`. More info https://stackoverflow.com/a/11480852/4458531 – NineCattoRules May 30 '19 at 08:29
  • You say "$_GET is not a function or language construct—it's just a variable (an array)" but we do not access values via index, instead you are accessing values via string. Is this more of a dictionary than an array? – ScottyBlades Oct 26 '19 at 22:53
  • 1
    @ScottyBlades I'm speaking in the context of PHP data structures. In PHP, an [array](https://www.php.net/manual/en/language.types.array.php) is an ordered list of key/value combinations. That differs from what other languages (e.g. C or JavaScript) call "array". – Álvaro González Oct 27 '19 at 12:02
31

Please post your code,

<?php
    echo $_GET['link'];
?>

or

<?php
    echo $_REQUEST['link'];
?>

do work...

MarcoS
  • 17,323
  • 24
  • 96
  • 174
24

Use this:

$parameter = $_SERVER['QUERY_STRING'];
echo $parameter;

Or just use:

$parameter = $_GET['link'];
echo $parameter ;
Muhammad Ashikuzzaman
  • 3,075
  • 6
  • 29
  • 53
21

To make sure you're always on the safe side, without getting all kinds of unwanted code insertion use FILTERS:

echo filter_input(INPUT_GET,"link",FILTER_SANITIZE_STRING);

More reading on php.net function filter_input, or check out the description of the different filters

patrick
  • 11,519
  • 8
  • 71
  • 80
  • Please note that this filter does not protect against any kind of attacks. This filter will however irreparably damage your data. Do not use it unless you know exactly what you are doing. – Dharman Apr 20 '21 at 18:10
10

The accepted answer is good. But if you have a scenario like this:

http://www.mydomain.me/index.php?state=California.php#Berkeley

You can treat the named anchor as a query string like this:

http://www.mydomain.me/index.php?state=California.php&city=Berkeley

Then, access it like this:

$Url = $_GET['state']."#".$_GET['city'];
Ben Harold
  • 6,242
  • 6
  • 47
  • 71
Philip E
  • 838
  • 9
  • 15
5

I was getting nothing for any $_GET["..."] (e.g print_r($_GET) gave an empty array) yet $_SERVER['REQUEST_URI'] showed stuff should be there. In the end it turned out that I was only getting to the web page because my .htaccess was redirecting it there (my 404 handler was the same .php file, and I had made a typo in the browser when testing).

Simply changing the name meant the same php code worked once the 404 redirection wasn't kicking in!

So there are ways $_GET can return nothing even though the php code may be correct.

cf-
  • 8,598
  • 9
  • 36
  • 58
user235510
  • 51
  • 1
  • 2
5
$Query_String  = explode("&", explode("?", $_SERVER['REQUEST_URI'])[1] );
var_dump($Query_String)

Array ( [ 0] => link=www.google.com )

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Saurabh Chandra Patel
  • 12,712
  • 6
  • 88
  • 78