Is it a bad practice to use a GET parameter (in URL) with no value?

前端 未结 3 1733
野性不改
野性不改 2021-01-01 16:07

I\'m in a little argument with my boss about URLs using GET parameters without value. E.g.

http://www.example.com/?logout

I see this kind of lin

3条回答
  •  萌比男神i
    2021-01-01 16:37

    A value is not required for the key to have any effect. It doesn't make the URL any less valid either, the URL RFC1738 does not list it as required part of the URL.

    If you don't really need a value, it's just a matter of preference.

    http://example.com/?logout
    

    Is just as much a valid URL as

    http://example.com/?logout=yes
    

    All difference that it makes is that if you want to make sure that the "yes" bit was absolutely set, you can check for it's value. Like:

    if(isset($_GET['logout']) && $_GET['logout'] == "yes") {
        // Only proceed if the value is explicitly set to yes
    

    If you just want to know if the logout key was set somewhere in the URL, it would suffice to just list the key with no value assigned to it. You can then check it like this:

    if(isset($_GET['logout'])) {
        // Continue regardless of what the value is set to (or if it's left empty)
    

提交回复
热议问题