What's wrong with using $_REQUEST[]?

后端 未结 16 1530
走了就别回头了
走了就别回头了 2020-11-22 03:53

I\'ve seen a number of posts on here saying not to use the $_REQUEST variable. I usually don\'t, but sometimes it\'s convenient. What\'s wrong with it?

16条回答
  •  被撕碎了的回忆
    2020-11-22 04:17

    I think there is no problem with $_REQUEST, but we must be careful when using it since it is a collection of variables from 3 sources (GPC).

    I guess $_REQUEST is still available to make old programs compatible with new php versions, but if we start new projects (including new libraries) I think we should not use $_REQUEST anymore to make the programs clearer. We should even consider deleting uses of $_REQUEST and replacing it with a wrapper function to make the program lighter, especially in processing large submitted text data, since $_REQUEST contains copies of $_POST.

    // delete $_REQUEST when program execute, the program would be lighter 
    // when large text submitted
    unset($_REQUEST);
    
    // wrapper function to get request var
    function GetRequest($key, $default = null, $source = '') 
    {
      if ($source == 'get') {
        if (isset($_GET[$key])) { 
          return $_GET[$key]; 
        } else { 
          return $default; 
        }
      } else if ($source == 'post') {
        if (isset($_POST[$key])) { 
          return $_POST[$key]; 
        } else { 
          return $default; 
        }
      } else if ($source == 'cookie') {
        if (isset($_COOKIE[$key])) { 
          return $_COOKIE[$key]; 
        } else { 
          return $default; 
        }
      } else {
        // no source specified, then find in GPC
        if (isset($_GET[$key])) {
          return $_GET[$key];     
        } else if (isset($_POST[$key])) {
          return $_POST[$key]; 
        } else if (isset($_COOKIE[$key])) {
          return $_COOKIE[$key]; 
        } else {
          return $default; 
        } 
      }
    }
    

提交回复
热议问题