When and why should $_REQUEST be used instead of $_GET / $_POST / $_COOKIE?

前端 未结 6 1460

Question in the title.

And what happens when all 3 of $_GET[foo], $_POST[foo] and $_COOKIE[foo] exist? Which one of them gets

6条回答
  •  旧巷少年郎
    2020-12-01 11:31

    $_REQUEST is only a shortcut to prevent you from testing post, get and cooking if the data can come from any of these.

    There are some pitfalls :

    • data are taken from GET, POST and finally COOKIE . The last override the first, so be careful with that.
    • REST architectures require to separate the POST and GET semantics, you can't rely on $_REQUEST in that case.

    Nevertheless, if you know what you're doing, then it's just another handy PHP trick.

    I'd use it if I wanted to quickly update a var that may come from several sources. E.G :

    • In your controller, to decide what page to serve without checking if the request come from a form action or a hypertext link.
    • To check if a session is still active regardless of the way session id are transmitted.

提交回复
热议问题