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 相关文章推荐
用Zend Encode编写开发PHP程序
Oct 09 PHP
支持oicq头像的留言簿(一)
Oct 09 PHP
CodeIgniter使用phpcms模板引擎
Nov 12 PHP
php二维数组转成字符串示例
Feb 17 PHP
php+ajax实现图片文件上传功能实例
Jun 17 PHP
php获取、检查类名、函数名、方法名的函数方法
Jun 25 PHP
Symfony的安装和配置方法
Mar 17 PHP
Laravel与CI框架中截取字符串函数
May 08 PHP
Thinkphp 空操作、空控制器、命名空间(详解)
May 05 PHP
PHP 断点续传实例详解
Nov 11 PHP
Laravel框架实现利用中间件进行操作日志记录功能
Jun 06 PHP
tp5.1 框架join方法用法实例分析
May 26 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
php判断页面是否是微信打开的示例(微信打开网页)
2014/04/25 PHP
PHP获取数组最大值下标的方法
2015/05/12 PHP
php实现购物车功能(以大苹果购物网为例)
2017/03/09 PHP
PHP中十六进制颜色与RGB颜色值互转的方法
2019/03/18 PHP
javascript网页关闭时提醒效果脚本
2008/10/22 Javascript
zShowBox 图片放大展示jquery版 兼容性
2011/09/24 Javascript
jQuery extend 的简单实例
2013/09/18 Javascript
JQuery实现动态适时改变字体颜色的方法
2015/03/10 Javascript
js网页滚动条滚动事件实例分析
2015/05/05 Javascript
JavaScript实现打开链接页面的方式汇总
2016/06/02 Javascript
浅谈JavaScript 标准对象
2016/06/02 Javascript
浅谈用Webpack路径压缩图片上传尺寸获取的问题
2018/02/22 Javascript
详解小程序原生使用ES7 async/await语法
2018/08/06 Javascript
微信小程序遍历Echarts图表实现多个饼图
2019/04/25 Javascript
layui+jquery支持IE8的表格分页方法
2019/09/28 jQuery
如何构建一个Vue插件并生成npm包
2020/10/26 Javascript
使用Node.js和Socket.IO扩展Django的实时处理功能
2015/04/20 Python
Python中列表的一些基本操作知识汇总
2015/05/20 Python
python实现的简单FTP上传下载文件实例
2015/06/30 Python
Python多线程下载文件的方法
2015/07/10 Python
深入浅析python定时杀进程
2016/06/06 Python
Python使用当前时间、随机数产生一个唯一数字的方法
2017/09/18 Python
浅谈python中copy和deepcopy中的区别
2017/10/23 Python
python中int与str互转方法
2018/07/02 Python
对python3 Serial 串口助手的接收读取数据方法详解
2019/06/12 Python
给 TensorFlow 变量进行赋值的方式
2020/02/10 Python
Python新手学习装饰器
2020/06/04 Python
Idea安装python显示无SDK问题解决方案
2020/08/12 Python
python音频处理的示例详解
2020/12/23 Python
python 检测nginx服务邮件报警的脚本
2020/12/31 Python
销售员态度差检讨书
2014/10/26 职场文书
2015年幼儿教师个人工作总结
2015/05/20 职场文书
亮剑精神观后感
2015/06/05 职场文书
入党宣誓大会后的感想
2015/08/10 职场文书
Java spring定时任务详解
2021/10/05 Java/Android
分享Python获取本机IP地址的几种方法
2022/03/17 Python