- 在此,整理一下平常工作中,常用的一些公共方法,方便后期开发使用
function showMsg($status = 1,$msg = '',$data = array()){
$result = array(
'status' => $status,
'msg' =>$msg,
'data' =>$data
);
return exit(json_encode($result,JSON_UNESCAPED_UNICODE));
}
function currArrExistsInOther($arr = [], $allArr = []){
if (!empty($arr) && !empty($allArr)) {
for ($i = 0; $i < count($arr); $i ++) {
if (!in_array($arr[$i], $allArr)) {
return false;
}
}
return true;
}else{
return false;
}
}
function code($url,$user_id,$recommend){
$user=M('users')->field('user_name')->where('user_id='.$user_id)->find();
$user_name=$user['user_name'];
$canshu="?r_ec=$recommend";
$url=$url.$canshu;
vendor("phpqrcode.phpqrcode");
$level = 'H';
$size = 4;
$path = "Public/code/".date("ymd")."/";
if(!is_dir($path))
{
mkdir($path);
}
$fileName = $path.$user_name.'.png';
$data['code']=$fileName;
M('users')->where('user_id='.$user_id)->save($data);
ob_end_clean();
QRcode::png($url, $fileName, $level, $size,1);
}
function is_weixin(){
if(strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessenger') === false ){
return false;
}else{
return ture;
}
}
function get_ip() {
static $ip;
if (isset($_SERVER)) {
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
$ip = $_SERVER["HTTP_CLIENT_IP"];
} else {
$ip = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("HTTP_CLIENT_IP")) {
$ip = getenv("HTTP_CLIENT_IP");
} else {
$ip = getenv("REMOTE_ADDR");
}
}
if (preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/', $ip)) {
return $ip;
} else {
return '127.0.0.1';
}
}
|