Asked  6 Months ago    Answers:  5   Viewed   40 times

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?

 Answers

77

$_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';
Tuesday, June 1, 2021
 
Webroots
answered 6 Months ago
33

Note: The following solution isn't ideal for high traffic situations.

$url = 'http://www.boligsiden.dk/viderestilling/992cff55882a40f79e64b0a25e847a69';

file_get_contents($url);
preg_match('/(Location:|URI:)(.*?)n/', implode("n", $http_response_header), $matches);

if (isset($matches[0]))
{
    echo $matches[0];
}

Here's what happens: file_get_contents() redirects and downloads the target website but writes the original response header into $http_response_header.

the preg_match tries to find the first "Location: x" match and returns it.

Wednesday, March 31, 2021
 
mcography
answered 9 Months ago
24

json_decode() expects a string, not a URL. You're trying to decode that url (and json_decode() will NOT do an http request to fetch the url's contents for you).

You have to fetch the json data yourself:

$json = file_get_contents('http://...'); // this WILL do an http request for you
$data = json_decode($json);
echo $data->{'token'};
Saturday, May 29, 2021
 
daiscog
answered 7 Months ago
57

Use the superglobal $_SERVER array, Location header and exit;

$port = '10500';
header('Location: '
    . ($_SERVER['HTTPS'] ? 'https' : 'http')
    . '://' . $_SERVER['HTTP_HOST'] . ':' . $port
    . $_SERVER['REQUEST_URI']);
exit;
Saturday, May 29, 2021
 
PHPWDev
answered 7 Months ago
43

I think the one of the easiest ways out would be to parse the string returned by URL.getQuery() as

public static Map<String, String> getQueryMap(String query) {  
    String[] params = query.split("&");  
    Map<String, String> map = new HashMap<String, String>();

    for (String param : params) {  
        String name = param.split("=")[0];  
        String value = param.split("=")[1];  
        map.put(name, value);  
    }  
    return map;  
}

You can use the map returned by this function to retrieve the value keying in the parameter name.

Wednesday, June 2, 2021
 
relyt
answered 6 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share