GET URL parameter in PHP

前端 未结 9 978
旧时难觅i
旧时难觅i 2020-11-22 07:32

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://loc         


        
相关标签:
9条回答
  • 2020-11-22 08:02

    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>
    
    0 讨论(0)
  • 2020-11-22 08:03

    $_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';
    
    0 讨论(0)
  • 2020-11-22 08:12

    Use this:

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

    Or just use:

    $parameter = $_GET['link'];
    echo $parameter ;
    
    0 讨论(0)
  • 2020-11-22 08:14

    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'];
    
    0 讨论(0)
  • 2020-11-22 08:18
    $Query_String  = explode("&", explode("?", $_SERVER['REQUEST_URI'])[1] );
    var_dump($Query_String)
    

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

    0 讨论(0)
  • 2020-11-22 08:19

    Please post your code,

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

    or

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

    do work...

    0 讨论(0)
提交回复
热议问题