檢查請求是GET還是POST [重複]

本文翻譯自:Check whether a request is GET or POST [duplicate]

Possible Duplicate: 可能重複:
PHP detecting request type (GET, POST, PUT or DELETE) PHP檢測請求類型(GET,POST,PUT或DELETE)

This should be an easy one. 這應該很簡單。

I have a script, and in the script I want to determine whether the request arrive via GET or POST method. 我有一個腳本,在腳本中我想確定請求是通過GET還是POST方法到達的。

What is the correct way to do it? 這樣做的正確方法是什麼?

I am thinking of using something like this 我正在考慮使用這樣的東西

if (isset($_POST)) {
    // do post
} else  {
    // do get
}

But deep in my heart I don't feel this is the right way. 但在我內心深處,我覺得這不是正確的方法。 Any idea? 任何的想法?


#1樓

參考:https://stackoom.com/question/5kxP/檢查請求是GET還是POST-重複


#2樓

使用$_SERVER['REQUEST_METHOD']


#3樓

Better use $_SERVER['REQUEST_METHOD'] : 最好使用$_SERVER['REQUEST_METHOD']

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // …
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章