现在很多app使用手机号码作为注册号码,需要验证手机验证码,特提供此demo。
此方法使用畅卓短信接口
DB Table
手机短信验证码数据库。也可以使用session储存,只需要修改模型文件mobile_verify.php。使用session存储更加高效和方便
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | CREATE TABLE `mobile_verify` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增ID',
 `mobile_phone` VARCHAR(15) NOT NULL COMMENT '用户手机号码',
 `code` VARCHAR(50) NOT NULL COMMENT '验证码',
 `add_time` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT '条件时间',
 PRIMARY KEY (`id`),
 INDEX `mobile_phone` (`mobile_phone`),
 INDEX `add_time` (`add_time`)
 )
 COMMENT='手机验证表'
 COLLATE='utf8_general_ci'
 ENGINE=InnoDB;
 
 | 
手机短信发送接口
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | $mobile_phone       = 13800138000;//用户手机//empty($mobile_phone) 验证是否手机号码
 
 include('mobile_varify.php');
 $mode = new mobile_varify();
 $code = $mode->getCode();    //生成随机码
 
 $res = $mode->saveVerifyInfo($mobile_phone, $code); //保存验证码到数据库
 
 if(empty($res)){
 return false;
 }
 
 include('czsmsclient.php');
 $czsmsclient = new czsmsclient();
 $sendCode = $czsmsclient->register_send($mobile_phone, $code); //发送随机码
 
 | 
验证接口
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | $user_phone = 13800138000;
 $auth_code = 1234;
 include('mobile_varify.php');
 $mode = new mobile_varify();
 $mobile_verify = $mode->checkCode($user_phone, $auth_code);
 if($mobile_verify == false){
 $errorMsg = $mode->getError();
 $this->ajaxReturn(0,$errorMsg);
 }
 
 | 
短信供应商,使用畅卓短信接口。文件名 czsmsclient.php
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 
 | <?php
 class czsmsclient{
 private $userid = '***';
 private $account = '***';
 private $password = '***';
 private $sign = '【螃蟹壳】';
 private $host = "http://sms.chanzor.com:8001/sms.aspx";
 
 
 
 
 
 
 
 
 function post($data, $target) {
 $url_info = parse_url($target);
 $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
 $httpheader .= "Host:" . $url_info['host'] . "\r\n";
 $httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
 $httpheader .= "Content-Length:" . strlen($data) . "\r\n";
 $httpheader .= "Connection:close\r\n\r\n";
 
 $httpheader .= $data;
 
 $fd = fsockopen($url_info['host'], 80);
 fwrite($fd, $httpheader);
 $gets = "";
 while(!feof($fd)) {
 $gets .= fread($fd, 128);
 }
 fclose($fd);
 return $gets;
 }
 
 
 
 
 
 public function getAmount(){
 $target = $this->host;
 $post_data = "action=overage&userid={$this->userid}&account={$this->account}"
 . "&password={$this->password}&mobile={$mobile}&sendTime={$time}"
 . "&content=".rawurlencode($content);
 $gets = $this->post($post_data, $target);
 $start=strpos($gets,"<?xml");
 $data=substr($gets,$start);
 $xml=simplexml_load_string($data);
 $return = (json_decode(json_encode($xml),TRUE));
 return $return;
 }
 
 
 
 
 
 public function amountWarning(){
 $return = $this->getAmount();
 if($return['overage'] <= 100){
 $user_email = '***@pangxieke.com';
 $mail_body = date('Y-m-d:H:i:s') . '手机验证码余额不足'
 . '现在账号余额条数' . $return['overage']
 . '请尽快向手机验证项目账号充值';
 
 
 $mailer = mailer::get_instance();
 $mailer->send($user_email, '手机验证码余额不足', $mail_body);
 }
 }
 
 
 public function send($mobile, $content, $time = ''){
 
 $target = $this->host;
 
 
 $content = $this->content($content);
 
 
 $post_data = "action=send&userid={$this->userid}&account={$this->account}"
 . "&password={$this->password}&mobile={$mobile}&sendTime={$time}"
 . "&content=".rawurlencode($content);
 
 $gets = $this->post($post_data, $target);
 $start=strpos($gets,"<?xml");
 $data=substr($gets,$start);
 $xml=simplexml_load_string($data);
 $return = (json_decode(json_encode($xml),TRUE));
 
 if($return['returnstatus'] == 'Success'){
 return true;
 
 
 
 
 
 
 
 }else{
 return false;
 }
 }
 
 
 
 
 
 
 
 
 public function register_send($mobile, $code){
 $content = '您的验证码是:'. $code;
 return $this->send($mobile, $content);
 }
 
 
 
 
 
 public function content($content){
 return $content . $this->sign;
 }
 
 
 }
 
 | 
模型文件mobile_verify.php
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 
 | <?php
 
 
 
 
 class mobile_verifyModel extends RelationModel {
 
 public $errorMsg;
 
 
 
 
 
 
 
 public function info($mobile_phone){
 $info = $this->where(array('mobile_phone'=>$mobile_phone))->find();
 return $info;
 }
 
 
 
 
 
 
 public function getCode(){
 return $this->generateCode(4);
 }
 
 
 
 
 
 
 
 function generateCode($length = 4) {
 return rand(pow(10,($length-1)), pow(10,$length)-1);
 }
 
 
 
 
 
 
 
 
 public function saveVerifyInfo($mobile_phone, $code){
 $exist = $this->info($mobile_phone);
 
 if(!$exist){
 $data = array();
 $data['mobile_phone'] = $mobile_phone;
 $data['code'] = $code;
 $data['add_time'] = time();
 $res = $this->add($data);
 
 }else{
 $data = array();
 $data['code'] = $code;
 $data['add_time'] = time();
 $res = $this->where(array('mobile_phone'=>$mobile_phone))->save($data);
 }
 return $res;
 }
 
 
 
 
 
 
 
 
 public function checkCode($mobile_phone, $code){
 
 $info = $this->info($mobile_phone);
 if(empty($info)){
 $this->errorMsg = '验证码错误';
 return false;
 }else if($info['code'] != $code){
 $this->errorMsg = '验证码错误';
 return false;
 }else if( $info['add_time'] < time()- 300){
 $this->errorMsg = '验证码已过期';
 return false;
 }else{
 return true;
 }
 }
 
 
 
 
 
 
 public function getError(){
 return $this->errorMsg;
 }
 
 }
 
 |