Parse URL in php

后端 未结 2 1522
小鲜肉
小鲜肉 2021-01-28 08:33

In php, if I wanted to parse a URL, such as www.site.com/index.php?foo=bar, I can do with the _POST variable. I can retrieve bar by _POST[\'foo\'].

2条回答
  •  失恋的感觉
    2021-01-28 09:27

    It's a misconception that requesting a URL like

    http://www.example.com/index.php?foo=bar
    

    would give you bar in $_POST['bar']. Url parameters will populate $_GET. Anything that's supposed to show up in $_POST has to be submitted in the Request body. See How are parameters sent in an HTTP POST request? for some details.

    With that clarified, empty URL parameters are not a problem at all. A URL like

    http://www.example.com/index.php?foo&bar
    

    will populate $_GET['foo'] and $_GET['bar'] with empty values.

提交回复
热议问题