php实现的IMEI限制的短信验证码发送类


Posted in PHP onMay 05, 2015

php实现的IMEI限制的短信验证码发送类

<?php
class Api_Sms{
  const EXPIRE_SEC = 1800;    // 过期时间间隔
  const RESEND_SEC = 60;     // 重发时间间隔
  const ONE_DAY_FREQ = 5;    // 每日向同一个手机号发短信的次数
  const ONE_DAY_IMEI_COUNT = 3; // 每日向同一个手机号发送短信的IMEI个数
 
  public $error = array();
 
 
  /**
   * 向指定手机号发送验证码
   * @param $mobile
   * @param $imei
   * @return bool
   */
  public function sendVerifyCode($mobile, $imei) {
    if(!$this->isMobile($mobile)) {
      $this->error = array('code' => -1, 'msg' => '这个手机号很奇葩哦,请正确输入后重试');
      return false;
    }
 
    $redis = Api_Common::redis();
    $vcKey = 'VC_'.$mobile;
    $limitKey = 'VC_LIMIT_'.$mobile;
 
    // 验证码重发限制
    $data = json_decode($redis->get($vcKey), true);
    if($data && time() < $data['resend_expire']) {
      $this->error = array('code' => -1, 'msg' => '短信已在1分钟内发出,请耐心等待');
      return false;
    }
 
    // 手机号及IMEI限制
    $sendCnt = $redis->zScore($limitKey, $imei);
    if($sendCnt && $sendCnt >= self::ONE_DAY_FREQ) {
      $this->error = array('code' => -1, 'msg' => '没收到短信?请稍等或检查短信是否被屏蔽');
      return false;
    }
    $imeiCnt = $redis->zCard($limitKey);
    if($imeiCnt >= self::ONE_DAY_IMEI_COUNT && !$sendCnt) {
      $this->error = array('code' => -1, 'msg' => '已超过验证码发送设备限制');
      return false;
    }
 
    // 获取验证码
    if(!$data) {
      $vc = strval(rand(100000, 999999));
      $data = array('vc' => $vc, 'resend_expire' => 0);
      $redis->set($vcKey, json_encode($data));
      $redis->expire($vcKey, self::EXPIRE_SEC); // 设置验证码过期时间
    }
    $vc = $data['vc'];
 
    $content = '安全验证码:'.$vc;
    $result = $this->send($mobile, $content);
    if($result) {
      // 重设重发时限
      $data['resend_expire'] = time() + self::RESEND_SEC;
      $ttl = $redis->ttl($vcKey);
      $redis->set($vcKey, json_encode($data));
      $redis->expire($vcKey, $ttl);
 
      // 设置手机号与IMEI限制
      $redis->zIncrBy($limitKey, 1, $imei);
      $redis->expireAt($limitKey, strtotime(date('Y-m-d',strtotime('+1 day'))));
    }
    return $result;
  }
 
  /**
   * 向指定手机号发送短信
   * @param $mobile
   * @param $content
   * @return bool
   */
  public function send($mobile, $content){
    // TODO 调用具体服务商API
    return true;
  }
 
  /**
   * 判断是否为合法手机号
   * @param $mobile
   * @return bool
   */
  private function isMobile($mobile) {
    if(preg_match('/^1\d{10}$/', $mobile))
      return true;
    return false;
  }
 
  /**
   * 验证短信验证码
   * @param $mobile
   * @param $vc
   * @return bool
   */
  public function checkVerifyCode($mobile, $vc) {
    $vcKey = 'VC_'.$mobile;
    $vcData = json_decode(Api_Common::redis()->get($vcKey), true);
    if($vcData && $vcData['vc'] === $vc) {
      return true;
    }
    return false;
  }
 
  /**
   * 清除验证码
   * @param $mobile
   */
  public function cleanVerifyCode($mobile) {
    $redis = Api_Common::redis();
    $vcKey = 'VC_'.$mobile;
    $limitKey = 'VC_LIMIT_'.$mobile;
    $redis->del($vcKey);
    $redis->del($limitKey);
  }
}

另付其他网友实现的短信验证码代码

<?
/*--------------------------------
功能:   中国短信网PHP HTTP接口 发送短信
修改日期:  2009-04-08
说明:   http://http.c123.com/tx/?uid=用户账号&pwd=MD5位32密码&mobile=号码&content=内容
状态:
  100 发送成功
  101 验证失败
  102 短信不足
  103 操作失败
  104 非法字符
  105 内容过多
  106 号码过多
  107 频率过快
  108 号码内容空
  109 账号冻结
  110 禁止频繁单条发送
  111 系统暂定发送
  112 号码不正确
  120 系统升级
--------------------------------*/
$uid = '9999';   //用户账号
$pwd = '9999';   //密码
$mobile = '13912341234,13312341234,13512341234,02122334444';  //号码
$content = '中国短信网PHP HTTP接口';    //内容
//即时发送
$res = sendSMS($uid,$pwd,$mobile,$content);
echo $res;
 
//定时发送
/*
$time = '2010-05-27 12:11';
$res = sendSMS($uid,$pwd,$mobile,$content,$time);
echo $res;
*/
function sendSMS($uid,$pwd,$mobile,$content,$time='',$mid='')
{
  $http = 'http://http.c123.com/tx/';
  $data = array
    (
    'uid'=>$uid,         //用户账号
    'pwd'=>strtolower(md5($pwd)),  //MD5位32密码
    'mobile'=>$mobile,        //号码
    'content'=>$content,     //内容
    'time'=>$time,    //定时发送
    'mid'=>$mid           //子扩展号
    );
  $re= postSMS($http,$data);     //POST方式提交
  if( trim($re) == '100' )
  {
    return "发送成功!";
  }
  else
  {
    return "发送失败! 状态:".$re;
  }
}
 
function postSMS($url,$data='')
{
  $row = parse_url($url);
  $host = $row['host'];
  $port = $row['port'] ? $row['port']:80;
  $file = $row['path'];
  while (list($k,$v) = each($data)) 
  {
    $post .= rawurlencode($k)."=".rawurlencode($v)."&"; //转URL标准码
  }
  $post = substr( $post , 0 , -1 );
  $len = strlen($post);
  $fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
  if (!$fp) {
    return "$errstr ($errno)\n";
  } else {
    $receive = '';
    $out = "POST $file HTTP/1.1\r\n";
    $out .= "Host: $host\r\n";
    $out .= "Content-type: application/x-www-form-urlencoded\r\n";
    $out .= "Connection: Close\r\n";
    $out .= "Content-Length: $len\r\n\r\n";
    $out .= $post;   
    fwrite($fp, $out);
    while (!feof($fp)) {
      $receive .= fgets($fp, 128);
    }
    fclose($fp);
    $receive = explode("\r\n\r\n",$receive);
    unset($receive[0]);
    return implode("",$receive);
  }
}
?>

以上所述就是本文的全部内容了,希望大家能够喜欢。

PHP 相关文章推荐
PHP4实际应用经验篇(6)
Oct 09 PHP
PHP数字格式化
Dec 06 PHP
fleaphp下不确定的多条件查询的巧妙解决方法
Sep 11 PHP
php截取后台登陆密码的代码
May 05 PHP
php class类的用法详细总结
Oct 17 PHP
php中读写文件与读写数据库的效率比较分享
Oct 19 PHP
PHP添加图片水印、压缩、剪切的封装类
Aug 17 PHP
thinkPHP简单导入和使用阿里云OSSsdk的方法
Mar 15 PHP
万能的php分页类
Jul 06 PHP
PHP设计模式之模板方法模式定义与用法详解
Apr 02 PHP
Phpstorm+Xdebug断点调试PHP的方法
May 14 PHP
PHP基于ip2long实现IP转换整形
Dec 11 PHP
php自定义hash函数实例
May 05 #PHP
php结合安卓客户端实现查询交互实例
May 05 #PHP
php为字符串前后添加指定数量字符的方法
May 04 #PHP
支持中文、字母、数字的PHP验证码
May 04 #PHP
php替换字符串中间字符为省略号的方法
May 04 #PHP
php中JSON的使用方法
Apr 30 #PHP
PHP使用CURL实现多线程抓取网页
Apr 30 #PHP
You might like
CodeIgniter使用phpcms模板引擎
2013/11/12 PHP
yii中widget的用法
2014/12/03 PHP
使用laravel的Eloquent模型如何获取数据库的指定列
2019/10/17 PHP
经典的解除许多网站无法复制文字的绝招
2006/12/31 Javascript
改善用户体验的五款jQuery插件分享
2011/05/22 Javascript
jquery插件开发注意事项小结
2013/06/04 Javascript
Javascript实现的Map集合工具类完整实例
2015/07/31 Javascript
JavaScript中SetInterval与setTimeout的用法详解
2015/11/10 Javascript
大型JavaScript应用程序架构设计模式
2016/06/29 Javascript
JavaScript-html标题滚动效果的简单实现
2016/09/08 Javascript
jQuery编写网页版2048小游戏
2017/01/06 Javascript
Angular2安装angular-cli
2017/05/21 Javascript
vue bus全局事件中心简单Demo详解
2018/02/26 Javascript
使用vue中的混入mixin优化表单验证插件问题
2019/07/02 Javascript
浅谈Vue3 Composition API如何替换Vue Mixins
2020/04/29 Javascript
基于Vue+Webpack拆分路由文件实现管理
2020/11/16 Javascript
编写Python脚本抓取网络小说来制作自己的阅读器
2015/08/20 Python
Python使用os模块和fileinput模块来操作文件目录
2016/01/19 Python
深入浅析python继承问题
2016/05/29 Python
python使用标准库根据进程名如何获取进程的pid详解
2017/10/31 Python
Python selenium 加载并保存QQ群成员,去除其群主、管理员信息的示例代码
2020/05/28 Python
python3 循环读取excel文件并写入json操作
2020/07/14 Python
matplotlib部件之矩形选区(RectangleSelector)的实现
2021/02/01 Python
移动web模拟客户端实现多方框输入密码效果【附代码】
2016/03/25 HTML / CSS
phonegap常用事件总结(必看篇)
2017/03/31 HTML / CSS
英国最大的体育&时尚零售公司:JD Sports
2017/12/13 全球购物
JSF界面控制层技术
2013/06/17 面试题
双十佳事迹材料
2014/01/29 职场文书
奠基仪式主持词
2014/03/20 职场文书
环保专项行动方案
2014/05/12 职场文书
求职信内容怎么写
2014/05/26 职场文书
天地会口号
2014/06/17 职场文书
教师党员学习十八届四中全会思想汇报
2014/11/03 职场文书
2015年世界环境日演讲稿
2015/03/18 职场文书
python使用openpyxl库读写Excel表格的方法(增删改查操作)
2021/05/02 Python
Nginx 匹配方式
2022/05/15 Servers