This article will share with you the php class that can be used to verify the text message verification code sent by the mobile phone number and IMEI. it is very practical. we recommend it to you here. if you need it, you can refer to it. Text message verification code sending class for IMEI restrictions implemented by php
<? Phpclass Api_Sms {const EXPIRE_SEC = 1800; // the expiration time interval const RESEND_SEC = 60; // The retransmission interval const ONE_DAY_FREQ = 5; // The number of messages sent to the same phone number every day const ONE_DAY_IMEI_COUNT = 3; // The number of IMEI messages sent to the same phone number every day public $ error = array (); /*** send verification code * @ param $ mobile * @ param $ imei * @ return bool */public function sendVerifyCode ($ mobile, $ imei) {if (! $ This-> isMobile ($ mobile) {$ this-> error = array ('code' =>-1, 'MSG '=>' this phone number is amazing, please enter the correct information and try again '); return false;} $ redis = Api_Common: redis (); $ vcKey = 'VC _'. $ mobile; $ limitKey = 'VC _ LIMIT _'. $ mobile; // verification code resending limit $ data = json_decode ($ redis-> get ($ vcKey), true); if ($ data & time () <$ data ['resend _ expire ']) {$ this-> error = array ('code' =>-1, 'MSG '=>' the text message has been sent within 1 minute. please wait patiently '); return false;} // The mobile phone number and IMEI limit $ s EndCnt = $ redis-> zScore ($ limitKey, $ imei); if ($ sendCnt & $ sendCnt> = self: ONE_DAY_FREQ) {$ this-> error = array ('code' =>-1, 'MSG '=>' didn't receive the text message? Please wait or check whether the text message is blocked '); return false;} $ imeiCnt = $ redis-> zCard ($ limitKey); if ($ imeiCnt> = self :: ONE_DAY_IMEI_COUNT &&! $ SendCnt) {$ this-> error = array ('code' =>-1, 'MSG '=>' has exceeded the verification code sending device limit '); return false ;} // Obtain the verification code if (! $ Data) {$ vc = strval (rand (100000,999 999); $ data = array ('VC '=> $ vc, 'resend _ expire' => 0 ); $ redis-> set ($ vcKey, json_encode ($ data); $ redis-> expire ($ vcKey, self: EXPIRE_SEC ); // Set the verification code expiration time} $ vc = $ data ['VC ']; $ content = 'security verification code :'. $ vc; $ result = $ this-> send ($ mobile, $ content); if ($ result) {// reset the retransmission time limit $ data ['resend _ expire '] = time () + self: RESEND_SEC; $ ttl = $ redis-> ttl ($ vcKey ); $ redis-> set ($ vcKey, json_encode ($ data); $ redis-> expire ($ vcKey, $ ttl ); // Set the mobile phone number and IMEI limit $ redis-> zIncrBy ($ limitKey, 1, $ imei); $ redis-> expireAt ($ limitKey, strtotime (date ('Y-m-D', strtotime ('+ 1 DAY');} return $ result ;} /*** send a text message to the specified mobile phone number * @ param $ mobile * @ param $ content * @ return bool */public function send ($ mobile, $ content) {// TODO calls the API of a specific service provider return true;}/*** determines whether the mobile phone number is valid * @ param $ mobile * @ return bool */private function isMobile ($ mobile) {if (preg_match ('/^ 1 \ d {10} $/', $ mobile) return true; return false ;} /*** verify the SMS verification code ** @ 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 ;} /*** clear the verification code * @ param $ mobile */public function cleanVerifyCode ($ mobile) {$ redis = Api_Common: redis (); $ vcKey = 'VC _'. $ mobile; $ limitKey = 'VC _ LIMIT _'. $ mobile; $ redis-> del ($ vcKey); $ redis-> del ($ limitKey );}}
Additional text message verification code implemented by other users
<? /* -------------------------------- Function: SMS sent by the php http interface of China SMS. modification date: Description: http://http.c123.com/tx/ ? Uid = user account & pwd = MD5 32-bit password & mobile = number & content = content status: 100 successful sending 101 verification failed 102 SMS less than 103 operation failed 104 illegal characters 105 too many content 106 too many numbers 107 too frequent too fast 108 number content blank 109 account frozen 110 prohibit frequent single message sending 111 system tentative sending 112 Incorrect number 120 system upgrade -------------------------------- */$ uid = '000000 '; // User account $ pwd = '000000'; // password $ mobile = '0000001234,13512341234, 12341234 '; // Number $ content = 'China sms php http interface '; // content // instant sending $ res = sendSMS ($ uid, $ pwd, $ mobile, $ content); echo $ res; // timed sending/* $ time = '2017-05-27 '; $ 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, // User account 'pwd' => strtolower (md5 ($ pwd )), // MD5 32-bit password 'mobile' => $ mobile, // number 'content' => $ content, // content 'time' => $ time, // regularly send 'mid '=> $ mid // subextension number); $ re = postSMS ($ http, $ data ); // submit in POST mode if (trim ($ re) = '000000') {return "sent successfully! ";} Else {return" failed to send! Status :". $ 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 conversion standard code} $ 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"; $ out. = $ post; fwrite ($ fp, $ out); while (! Feof ($ fp) {$ receive. = fgets ($ fp, 128);} fclose ($ fp); $ receive = explode ("\ r \ n", $ receive ); unset ($ receive [0]); return implode ("", $ receive) ;}}?>
The above is all the content of this article. I hope you will like it.