GET URL parameter in PHP

PhpUrlRedirect

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

$_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';

Solution 2 - Php

Please post your code,

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

or

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

do work...

Solution 3 - Php

Use this:

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

Or just use:

$parameter = $_GET['link'];
echo $parameter ;

Solution 4 - Php

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

Solution 5 - Php

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'];

Solution 6 - Php

$Query_String  = explode("&", explode("?", $_SERVER['REQUEST_URI'])[1] );
var_dump($Query_String)

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

Solution 7 - Php

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.

Solution 8 - Php

This is what I done with the problem, and I went a step further and used that parameter to set a cookie, and then redirect to a clean URL.

if (isset ($_GET['referrer'])) {
    $referrer = $_GET['referrer'];

//    check db if refer exists
    $shareLinkTrue = (new queries)->checkNewReferralExists($conn, $referrer);
//    if exists, set cookie
    if ($shareLinkTrue == 1) {
        setcookie('_referrer_link', $referrer, 1,
            isset($params['path']),
            isset($params['domain']),
            isset($params['secure']),
            isset($params['httponly']));

        header('Location: register.php');

    }

}

Works perfect

Solution 9 - Php

As Alvaro said, $_GET is not a function but an array containing the parameters So you can retrieve one element from that array using

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

Expected OP:

www.google.com

Solution 10 - Php

Whomever gets nothing back, I think he just has to enclose the result in html tags,

Like this:

<html>
<head></head>
<body>
<?php
echo $_GET['link'];
?>
<body>
</html>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFeras OdehView Question on Stackoverflow
Solution 1 - PhpÁlvaro GonzálezView Answer on Stackoverflow
Solution 2 - PhpMarcoSView Answer on Stackoverflow
Solution 3 - PhpMuhammad AshikuzzamanView Answer on Stackoverflow
Solution 4 - PhppatrickView Answer on Stackoverflow
Solution 5 - PhpphilView Answer on Stackoverflow
Solution 6 - PhpSaurabh Chandra PatelView Answer on Stackoverflow
Solution 7 - Phpuser235510View Answer on Stackoverflow
Solution 8 - PhpSpinstazView Answer on Stackoverflow
Solution 9 - PhpOmer HijaziView Answer on Stackoverflow
Solution 10 - PhpbalazonView Answer on Stackoverflow