PHP judges the request type (ajax|get|post|cli)

        PHP determines the request type, which can be achieved through the parameters related to $_SERVER.

This is very common in the reuse of some request code. The specific code is as follows:


  1. /**
  2. * @todo : determine whether it is a post
  3. */
  4. if(!function_exists('is_post')){
  5. function is_post ()
  6. {
  7. return isset($_SERVER['REQUEST_METHOD']) && strtoupper($_SERVER['REQUEST_METHOD'])=='POST';
  8. }
  9. }
  10. /**
  11. * @todo : determine whether it is get
  12. */
  13. if(!function_exists('is_get')){
  14. function is_get()
  15. {
  16. return isset($_SERVER['REQUEST_METHOD']) && strtoupper($_SERVER['REQUEST_METHOD'])=='GET';
  17. }
  18. }
  19. /**
  20. * @todo : determine whether it is ajax
  21. */
  22. if(!function_exists('is_ajax')){
  23. function is_ajax()
  24. {
  25. return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtoupper($_SERVER['HTTP_X_REQUESTED_WITH'])=='XMLHTTPREQUEST';
  26. }
  27. }
  28. /**
  29. * @todo : determine whether it is in command line mode
  30. */
  31. if(!function_exists('is_cli')){
  32. function is_cli ()
  33. {
  34. return (PHP_SAPI === 'cli' OR defined('STDIN'));
  35. }
  36. }



Related: PHP judges the request type (ajax|get|post|cli)