php get variables as key with no value assigned

前端 未结 5 1536
执笔经年
执笔经年 2021-01-12 11:45

If I enter the following into the browser:

   http://domain.com/script.php?1234

And script.php has the following script:

           


        
相关标签:
5条回答
  • 2021-01-12 11:53

    This is correct. The variable is called 1234 and it has no value. That means

    GET['1234'] == '';
    

    You could as well write http://example.com?1234=10 Then the result would be

    GET['1234'] == '10';
    
    0 讨论(0)
  • 2021-01-12 11:54

    My question is, is this officially correct or it's poor programming?

    Query-parameter-without-value is not documented to work, though it probably will continue to in practice.

    If you just want to grab the whole query string without parsing it as parameters, the best thing to do would be to say so directly:

    echo $_SERVER['QUERY_STRING']
    

    (Whatever you do, don't echo it directly though! Either sanitise the value by eg. converting it to an integer, or, if you want to allow an arbitrary string, output it escaped suitably for JavaScript, by backslash-escaping quotes and backslashes.)

    0 讨论(0)
  • 2021-01-12 12:01

    Nothing can be officially correct, if it works for you then by all means use it. Btw the issue with that is matching what value is what.

    When you have example.com?1234&4567 Then you will have two values with no key name to tell you what is what.

    But if you want to do away with ugly url's, you should look into mod_rewrite

    Beginners guide

    Another beginners guide

    0 讨论(0)
  • 2021-01-12 12:05

    You could as well do

    echo $_SERVER['QUERY_STRING']
    

    As for creating friendly URLs, there are better ways to do it.

    See e.g.: $_GET and URL Rewriting for PHP

    0 讨论(0)
  • 2021-01-12 12:12

    You shouldn't use this approach. If you don't want your variables to be shown you can use URL rewrite to make the URL look good. Also, don't forget to sanitize your $_GET :)

    Actually, in your example, 1234 is the variable you send through GET, with no value, if you want to look it this way.

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