IF
为什么要写这些呢,因为我只会一些很基本的判断id或者其他的有没有传过来,觉得想写得高端一点,借鉴公司同事的写法,简直不能再棒了。
//某个控制器的代码:
public function info(){
$id = isset($_GET['id']) ? I('get.id') : 0;
0==intval($id) && showTips(U('Activity/index'),'您要查找的信息丢失了');
$list = M('active')->where('issh=1 AND endTime>='.time().' AND id='.$id)->order('startTime desc')->find();
!$list && showTips(U('Activity/index'),'您要查找的信息丢失了');
}
intval 变量转成整数类型。
语法: int intval(mixed var, int [base]);
返回值: 整数
变量说明:本函数可将变量转换成整数类型,可省略的参数 base 是转换的基底,默认值为 10。转换的变量 var 可以为数组或类之外的任何类型变量。
showTips是写在公共文件common里面的一个方法,如下:
/**
***操作提示**
**/
function showTips($backurl="/index.php?s=/Wap/Index/index.html",$content,$time=3) {
//header("Location:".$backurl);exit();
redirect($backurl, $time, '<!doctype html><html><head><meta charset="utf-8"><title>标题</title><meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"><meta content="yes" name="apple-mobile-web-app-capable"><meta content="black" name="apple-mobile-web-app-status-bar-style"><meta content="telephone=no" name="format-detection"><meta http-equiv="X-UA-Compatible" content="IE=edge"></head><body style=" background:#efefef;"><div style="font-size:16px; color:#000; text-align:center; line-height:180%; padding-top:30px;">'.$content.'<br/><i style=" margin-right:5px;"><img src="/Public/wap/img/notch.png"/></i>页面正在跳转中,请稍后...</div></body></html><style>div i{ -webkit-animation:an_rotate 2s linear infinite;}@-webkit-keyframes an_rotate{0%{ -webkit-transform:rotate(0);} 50% { -webkit-transform:rotate(180deg);}100%{ -webkit-transform:rotate(360deg);}}</style>');
exit();
}
2、如果是微信相关,也需要一些基本的安全判断,因为总是有些讨厌的人,攻击我写得不怎么样的程序。可以在每个控制器的入口写前置方法,比如:
//某控制器
private $userInfo = false;
public function _initialize(){
chkPost();
}
2.1 如图:
/**
*禁止非本域名提交
**/
function chkPost() {
$send_request = $_SERVER['HTTP_REFERER'];
$http_host = $_SERVER['HTTP_HOST'];
if ( $send_request == "" || $http_host != substr($send_request,7,strlen($http_host)) ) { exit('{"title":"ERROR","id":"-1","content":"Not submit a request from an external."}'); }
}
我同事都把ajax写在一个控制器里面了,专门就叫ajaxController了。然后发请求到后台后,直接后台处理后,按照统一一个格式输出,还是很方便的。
public function lizi(){
$username = mb_substr(trim(I("post.username")),0,20,'utf-8');
(empty($username) || '' == $username ) && exit('{"State":-2,"MSG":"姓名不能为空"}');
if('' !== $mobile){
!funRegMobile($mobile) && exit('{"State":-4,"MSG":"手机号码格式不正确"}');
$ishave = M('vip')->where("mobile='".$mobile."' AND id<>".$userInfo["id"])->find();
$ishave && exit('{"State":-5,"MSG":"手机号码被占用"}');
}else{
exit('{"State":-5,"MSG":"请填写电话号码"}');
}
!strtotime($age) && exit('{"State":-3,"MSG":"出生日期格式不正确"}');
}
2.1.1 下面是一个很全的ajax请求后处理的function
/***设置用户地理位置经纬度**/
public function setUserLocation(){
$this->isSignin();
$lat = trim(I("post.lat"));
$lng = trim(I("post.lng"));
$precision = trim(I("post.precision"));
(!is_numeric($lat) || !is_numeric($lng) || !is_numeric($precision)) && exit('{"State":-1,"MSG":"错误的地理位置"}');
$res = "做save操作";
!$res ? exit('{"State":-2,"MSG":"设置地理位置失败"}') : exit('{"State":1,"MSG":""}');
}
2.2.1 一个比较普通的例子,其实我并没有搞懂为什么要验证一下字符。代码如:
/****发生消息***/
public function sendMsg(){
$this->isSignin();
$toId = is_numeric($_POST['toId']) ? $_POST['toId'] : 0;
$msg = getShort(trim(I("post.msg")),1000);
0 == intval($toId) && exit('{"State":-1,"MSG":"非法的链接"}');
$toUsre = M('')->where('id='.$toId)->find();
!$toUsre && exit('{"State":-2,"MSG":"用户不存在"}');
(!$msg || '' == $msg) && exit('{"State":-3,"MSG":"消息不能为空"}');
$userInfo = session('wcInfo');
$res = M('')->add(array('fromId'=>$userInfo['id'],'toId'=>$toId,'msg'=>$msg,'createdAt'=>time()));
exit($res ? '{"State":1,"MSG":""}' : '{"State":0,"MSG":"发送失败"}');
}
上面的方法:getShort()是写在公共的大家都能用的方法里面。就是function.php
// 截取内容
function getShort($str, $length = 40, $ext = '') {
$str = htmlspecialchars ( $str );
$str = strip_tags ( $str );
$str = htmlspecialchars_decode ( $str );
$strlenth = 0;
$out = '';
preg_match_all ( "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/", $str, $match );
foreach ( $match [0] as $v ) {
preg_match ( "/[\xe0-\xef][\x80-\xbf]{2}/", $v, $matchs );
if (! empty ( $matchs [0] )) {
$strlenth += 1;
} elseif (is_numeric ( $v )) {
$strlenth += 0.5; // 字符字节长度比例 汉字为1
} else {
$strlenth += 0.5; // 字符字节长度比例 汉字为1
}
if ($strlenth > $length) {
$output .= $ext;
break;
}
$output .= $v;
}
return $output;
}