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:
- /**
- * @todo : determine whether it is a post
- */
- if(!function_exists('is_post')){
- function is_post ()
- {
- return isset($_SERVER['REQUEST_METHOD']) && strtoupper($_SERVER['REQUEST_METHOD'])=='POST';
- }
- }
-
- /**
- * @todo : determine whether it is get
- */
- if(!function_exists('is_get')){
- function is_get()
- {
- return isset($_SERVER['REQUEST_METHOD']) && strtoupper($_SERVER['REQUEST_METHOD'])=='GET';
- }
- }
-
- /**
- * @todo : determine whether it is ajax
- */
- if(!function_exists('is_ajax')){
- function is_ajax()
- {
- return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtoupper($_SERVER['HTTP_X_REQUESTED_WITH'])=='XMLHTTPREQUEST';
- }
- }
-
- /**
- * @todo : determine whether it is in command line mode
- */
- if(!function_exists('is_cli')){
- function is_cli ()
- {
- return (PHP_SAPI === 'cli' OR defined('STDIN'));
- }
- }