码迷,mamicode.com
首页 > Web开发 > 详细

php判断请求类型(ajax|get|post|cli)

时间:2018-07-27 19:19:06      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:pre   类型   def   令行   命令   function   pos   http   exists   

    php判断请求类型,可以通过 $_SERVER 相关的参数来实现,

这个很在对某些请求代码复用里面很常用。具体代码如下:

/**
*@todo: 判断是否为post
*/
if(!function_exists(‘is_post‘)){
function is_post()
{
return isset($_SERVER[‘REQUEST_METHOD‘]) && strtoupper($_SERVER[‘REQUEST_METHOD‘])==‘POST‘;
}
}

/**
*@todo: 判断是否为get
*/
if(!function_exists(‘is_get‘)){
function is_get()
{
return isset($_SERVER[‘REQUEST_METHOD‘]) && strtoupper($_SERVER[‘REQUEST_METHOD‘])==‘GET‘;
}
}

/**
*@todo: 判断是否为ajax
*/
if(!function_exists(‘is_ajax‘)){
function is_ajax()
{
return isset($_SERVER[‘HTTP_X_REQUESTED_WITH‘]) && strtoupper($_SERVER[‘HTTP_X_REQUESTED_WITH‘])==‘XMLHTTPREQUEST‘;
}
}

/**
*@todo: 判断是否为命令行模式
*/
if(!function_exists(‘is_cli‘)){
function is_cli()
{
return (PHP_SAPI === ‘cli‘ OR defined(‘STDIN‘));
}
}

php判断请求类型(ajax|get|post|cli)

标签:pre   类型   def   令行   命令   function   pos   http   exists   

原文地址:https://www.cnblogs.com/jjxhp/p/9379112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!