Text message Verification Code sending class for IMEI restrictions implemented by php, imei Verification Code

Source: Internet
Author: User

Text message Verification Code sending class for IMEI restrictions implemented by php, imei Verification Code

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: China SMS network php http interface sent sms modify 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, // timed sending of 'mid '=> $ mid // subextension number ); $ re = postSMS ($ http, $ dat A); // submit if (trim ($ re) = '000000') {return "successfully sent in POST mode! ";} 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.