发送手机验证码,验证手机验证码,包括数据表的设计
生活随笔
收集整理的這篇文章主要介紹了
发送手机验证码,验证手机验证码,包括数据表的设计
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
表結(jié)構(gòu):https://download.csdn.net/download/weixin_42330073/10848493
<?php/*** 發(fā)送手機(jī)驗(yàn)證碼*/public function sendMobileCode(){Db::startTrans();$mobile = $this->request->param('mobile','');$mc = new MobileCodeModel();$mobile_code = rand(100000, 999999);$search = '/^0?1[3|4|5|6|7|8][0-9]\d{8}$/';if (!preg_match($search,$mobile)) {$this->apiResponse(0,'手機(jī)號格式有誤','');}$content = "【振威】您的驗(yàn)證碼為".$mobile_code.",如非本人操作請忽略本短信!";$info = $mc->where(['mobile' => $mobile,'create_date' => date('Y-m-d')])->find();if($info){if(time() < $info['create_time']+60 and $info['is_use'] == 0){$this->apiResponse(0,'不能頻繁發(fā)送驗(yàn)證碼','');}if($info['count'] > 10){$this->apiResponse(0,'今天發(fā)送驗(yàn)證碼的次數(shù)已達(dá)到了上限','');}$res = $mc->where('id',$info['id'])->data(['mobile' => $mobile,'mobile_code' => $mobile_code,'is_use' => 0,'expire_time' => time()+300,'count' => $info['count'] +1])->update();}else{$res = $mc->insert(['mobile' => $mobile,'mobile_code' => $mobile_code,'is_use' => 0,'expire_time' => time()+300,'count' => 1,'create_time' => time(),'create_date' => date('Y-m-d')]);}if($res){//發(fā)送驗(yàn)證碼$is_ok = substr($this->sendCode($mobile,$content),0,1);if($is_ok != 1){Db::rollback();$this->apiResponse(0,'短信發(fā)送失敗','');}Db::commit();$this->apiResponse(1,'發(fā)送成功','');}}/*** 驗(yàn)證手機(jī)驗(yàn)證碼*/public function validateMobileCode($post){$mc = new MobileCodeModel();if(empty($post['mobile'])){$this->apiResponse(0,'缺少必要參數(shù):MOBILE','');}if(empty($post['mobile_code'])){$this->apiResponse(0,'缺少必要參數(shù):MOBILE_CODE','');}$res_find = $mc->where(['mobile' => $post['mobile'],'mobile_code' => $post['mobile_code'],'is_use' => 0,'create_date' => date('Y-m-d'),'expire_time' => ['gt',time()]])->find();if($res_find){$res_update = $mc->where('id',$res_find['id'])->setField('is_use',1);if($res_update){return true;}}else{$this->apiResponse(0,'驗(yàn)證未通過',$post);}}/*** 發(fā)送手機(jī)驗(yàn)證碼* @param $mobile* @param $code* @return mixed*/private function sendCode($mobile,$content){date_default_timezone_set('PRC');//設(shè)置時區(qū)$url = "http://www.ztsms.cn/sendNSms.do";//提交地址$username = Config::get('MB_USERNAME');//用戶名$password = Config::get('MB_PASSWORD');//原密碼$data = array('content' => $content,//短信內(nèi)容'mobile' => $mobile,//手機(jī)號碼'productid' => '676767',//產(chǎn)品id'xh' => ''//小號);$isTranscoding = false;$data['content'] = $isTranscoding === true ? mb_convert_encoding($data['content'], "UTF-8") : $data['content'];$data['username']=$username;$data['tkey'] = date('YmdHis');$data['password'] = md5(md5($password) . $data['tkey']);$curl = curl_init();// 啟動一個CURL會話curl_setopt($curl, CURLOPT_URL, $url); // 要訪問的地址curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 對認(rèn)證證書來源的檢查curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 從證書中檢查SSL加密算法是否存在curl_setopt($curl, CURLOPT_POST, true); // 發(fā)送一個常規(guī)的Post請求curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的數(shù)據(jù)包c(diǎn)url_setopt($curl, CURLOPT_TIMEOUT, 30); // 設(shè)置超時限制防止死循環(huán)curl_setopt($curl, CURLOPT_HEADER, false); // 顯示返回的Header區(qū)域內(nèi)容curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 獲取的信息以文件流的形式返回$result = curl_exec($curl); // 執(zhí)行操作return $result;}?
總結(jié)
以上是生活随笔為你收集整理的发送手机验证码,验证手机验证码,包括数据表的设计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python网络爬虫简单教程——第一部
- 下一篇: 嵌入式linux之go语言开发(三)卡库