在目录 /模块/Common/ 中建立“function.php”,代码如下:
<?php //公用函数库 //前台用户密码生成 function getMemberPWD($pwd){ return md5('!lYg_Bho0p~'.$pwd); } //保留两位小数,可供模板中使用 <{$price|rnd2=###}> function rnd2($p){ return sprintf("%.2f",(float)$p); } //电话号码检测 function cm_cktell($tell){ if(!preg_match("/^1[34578]{1}\d{9}$/",$tell)){ return false; } return true; } //生成本地交易订单号 function cm_uniqid(){ $str = time().'-'.rand(1000,9999).'-'.rand(0,1000).'-'.uniqid(); return md5($str); } //请求url 返回json格式后转为对象 function cm_geturljson($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); } //判断是否是手机 function cm_ismobile(){ $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $is_pc = (strpos($agent, 'windows nt')) ? true : false; $is_mac = (strpos($agent, 'mac os')) ? true : false; $is_iphone = (strpos($agent, 'iphone')) ? true : false; $is_android = (strpos($agent, 'android')) ? true : false; $is_ipad = (strpos($agent, 'ipad')) ? true : false; if($is_pc){ return false; } if($is_mac){ return false; } if($is_iphone){ return true; } if($is_android){ return true; } if($is_ipad){ return true; } } ?>
使用方法:通过方法名直接调用,如:
echo cm_ismobile()?"是手机":"不是手机";
同时以上方法还可以在模板中使用,参见:
function rnd2(){...}