Among $_REQUEST, $_GET and $_POST which one is the fastest?

后端 未结 15 2245
小鲜肉
小鲜肉 2020-11-22 05:04

Which of these code will be faster?

$temp = $_REQUEST[\'s\'];

or

if (isset($_GET[\'s\'])) {
  $temp = $_GET[\'s\'];
}
else          


        
相关标签:
15条回答
  • 2020-11-22 05:50

    I only ever use _GET or _POST. I prefer to have control.

    What I don't like about either code fragment in the OP is that they discard the information on which HTTP method was used. And that information is important for input sanitization.

    For example, if a script accepts data from a form that's going to be entered into the DB then the form had better use POST (use GET only for idempotent actions). But if the script receives the input data via the GET method then it should (normally) be rejected. For me, such a situation might warrant writing a security violation to the error log since it's a sign somebody is trying something on.

    With either code fragment in the OP, this sanitization wouldn't be possible.

    0 讨论(0)
  • 2020-11-22 05:53

    There are certain security concerns involved as a hacker can set a cookie that will override a $_POST or $_GET value. If you handle sensitive data, I would not recommend using $_REQUEST. – Xandor

    you can't be used $_GET alternative of $_POST on some case.

    When ??

    • when you want to upload a file.
    • when you don't won't to show a data in url.

    GET also has limits on the amount of information to send. The limitation is about 2000 characters.

    Other thing's there are few case when you can't retrieve a data using $_POST

    When ?

    • when data is passed in URL.

    For Rest Service

    `GET` - Provides a read only access to a resource.
    
    `PUT` - Used to create a new resource.
    

    there is nothing be wrong to use $_REQUEST.

    But the way to do that is to check $_SERVER['REQUEST_METHOD'] explicitly, not rely on $_POST being empty for a GET.

    0 讨论(0)
  • 2020-11-22 05:54

    I'd suggest using $_POST and $_GET explicitly.

    Using $_REQUEST should be unnecessary with proper site design anyway, and it comes with some downsides like leaving you open to easier CSRF/XSS attacks and other silliness that comes from storing data in the URL.

    The speed difference should be minimal either way.

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