判断post,ajax,get请求的方法

Posted 绝技小嗨皮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了判断post,ajax,get请求的方法相关的知识,希望对你有一定的参考价值。

判断post,ajax,get请求的方法


define('IS_GET',isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] == 'GET' : false);

define('IS_POST',isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] == 'POST' : false);

define('IS_AJAX',isset($_SERVER['HTTP_REQUEST_TYPE']) && $_SERVER['HTTP_REQUEST_TYPE'] == 'ajax'?true:false);

补充:


function is_ajax()

{

  return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtoupper($_SERVER['HTTP_X_REQUESTED_WITH'])=='XMLHTTPREQUEST';

}

function is_cli()

{
  return (php_SAPI === 'cli' OR defined('STDIN'));

}

php中获取客户端真实ip地址的方法


function get_real_ip()

{
   $ip=false;
   if(!empty($_SERVER['HTTP_CLIENT_IP'])){
       $ip=$_SERVER['HTTP_CLIENT_IP'];
   }
   if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
       $ips=explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
       if($ip){ array_unshift($ips, $ip); $ip=FALSE; }
       for ($i=0; $i < count($ips); $i++){
           if(!eregi ('^(10│172.16│192.168).', $ips[$i])){
               $ip=$ips[$i];
               break;
           }
       }
   }
   return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}

function get_real_ip(){
    static $realip;
    if(isset($_SERVER)){
        if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
            $realip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }else if(isset($_SERVER['HTTP_CLIENT_IP'])){
            $realip=$_SERVER['HTTP_CLIENT_IP'];
        }else{
            $realip=$_SERVER['REMOTE_ADDR'];
        }
    }else{
        if(getenv('HTTP_X_FORWARDED_FOR')){
            $realip=getenv('HTTP_X_FORWARDED_FOR');
        }else if(getenv('HTTP_CLIENT_IP')){
            $realip=getenv('HTTP_CLIENT_IP');
        }else{
            $realip=getenv('REMOTE_ADDR');
        }
    }
    return $realip;
}

function is_ip($str){
    $ip=explode('.',$str);
    for($i=0;$i<count($ip);$i++){
        if($ip[$i]>255){
            return false;
        }
    }
    return preg_match('/^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$/',$str);
}

function getIp(){
    $ip='未知IP';
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        return is_ip($_SERVER['HTTP_CLIENT_IP'])?$_SERVER['HTTP_CLIENT_IP']:$ip;
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        return is_ip($_SERVER['HTTP_X_FORWARDED_FOR'])?$_SERVER['HTTP_X_FORWARDED_FOR']:$ip;
    }else{
        return is_ip($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:$ip;
    }
}

/**
 * 获取客户端IP地址
 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
 * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
 * @return mixed
 */
function get_client_ip($type = 0,$adv=false) {
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if($adv){
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos    =   array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip     =   trim($arr[0]);
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip     =   $_SERVER['HTTP_CLIENT_IP'];
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip     =   $_SERVER['REMOTE_ADDR'];
        }
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}

php添加访问授权的方法


//允许所有域名的请求访问

header('Access-Control-Allow-Origin:*');

//允许百度的请求访问

header('Access-Control-Allow-Origin:https://www.baidu.com');

 

header('Access-Control-Allow-Methods:*');

header('Access-Control-Allow-Headers:x-requested-with,content-type');

补充:


$ret = [
  'name' => isset($_POST['name'])? $_POST['name'] : '',
  'gender' => isset($_POST['gender'])? $_POST['gender'] : ''
];

header('content-type:application:json;charset=utf8');

$origin = isset($_SERVER['HTTP_ORIGIN'] ? $_SERVER['HTTP_ORIGIN'] : '');

$allow_origin = [

  'http://www.baidu.com',

  'http://www.jjxhp.com'

];

if (in_array($origin, $allow_origin)) {  

  header('Access-Control-Allow-Origin:'.$origin);

  header('Access-Control-Allow-Methods:*');

  header('Access-Control-Allow-Headers:x-requested-with,content-type');

}

echo json_encode($ret);

以上是关于判断post,ajax,get请求的方法的主要内容,如果未能解决你的问题,请参考以下文章

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

AJAX_get请求和post请求实例代码

Ajax请求的Json解析以及Post与Get方法

AJAX get() 和 post() 方法

jQuery – AJAX get() 和 post() 方法

Ajax同步异步;get和post