Php-implemented IMEI-restricted text message verification code sending Class php-implemented IMEI-restricted text message verification code sending class
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;} // mobile phone ID and IMEI limit $ sendCnt = $ 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 ''=>'' exceeds the device limit for sending the verification code ''); 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
$ Uid, // User account ''pwd' => strtolower (md5 ($ pwd), // MD5 32-bit password ''mobil'' => $ mobile, // number ''content'' => $ content, // content ''Time'' => $ time, // periodically send the ''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) ;}}?>
Reprinted from: http://www.aspnetjia.com