PHP implementation to verify the mobile phone number and IMEI SMS verification Code

Source: Internet
Author: User
Tags php class
This article to you to share is can test mobile phone number and IMEI SMS Verification code sent PHP class, very practical, here is recommended to everyone, there is a need for small partners to refer to the next.

PHP implementation of IMEI limit SMS verification code send class

<?phpclass api_sms{Const EXPIRE_SEC = 1800;     Expiration time interval Const RESEND_SEC = 60;    Re-send interval const ONE_DAY_FREQ = 5; The number of daily SMS messages to the same mobile phone number const ONE_DAY_IMEI_COUNT = 3;    The number of IMEI messages sent daily to the same mobile phone number public $error = Array (); /** * Send a verification code to the designated mobile phone number * @param $mobile * @param $imei * @return BOOL */Public Function Sendverifycode ($mobile, $im EI) {if (! $this->ismobile ($mobile)) {$this->error = array (' Code ' +-1, ' msg ' = ') This mobile phone number is very wonderful Oh, please enter it correctly and try again '      );    return false;    } $redis = Api_common::redis ();    $vcKey = ' vc_ '. $mobile;     $limitKey = ' vc_limit_ '. $mobile;    Verification code re-send Limit $data = Json_decode ($redis->get ($vcKey), true); if ($data && time () < $data [' Resend_expire ']) {$this->error = array (' Code ' +-1, ' msg ' = = ' SMS already in 1      In minutes, please wait patiently ');    return false;    }//Mobile number and IMEI limit $sendCnt = $redis->zscore ($limitKey, $imei); if ($sendCnt && $sendCnt >= self::one_day_freq) {$this->error = array (' Code ' +-1, ' msg ' = ' + ' forfeited to SMS? Please wait or check if SMS is blocked ');    return false;    } $imeiCnt = $redis->zcard ($limitKey); if ($imeiCnt >= self::one_day_imei_count &&! $sendCnt) {$this->error = array (' Code ' = 1, ' msg ' =&G T      ' has exceeded the verification code sending device limit ');    return false;      }//Get the CAPTCHA if (! $data) {$VC = Strval (rand (100000, 999999));      $data = Array (' VC ' = = $VC, ' resend_expire ' = 0);      $redis->set ($vcKey, Json_encode ($data)); $redis->expire ($vcKey, self::expire_sec);     Set verification code Expiration Time} $VC = $data [' VC '];    $content = ' Security code: '. $VC;    $result = $this->send ($mobile, $content);      if ($result) {//Resets $data [' resend_expire '] = time () + self::resend_sec;      $ttl = $redis->ttl ($vcKey);      $redis->set ($vcKey, Json_encode ($data));       $redis->expire ($vcKey, $ttl);      Set the phone number with the IMEI limit $redis->zincrby ($limitKey, 1, $imei);    $redis->expireat ($limitKey, Strtotime (date (' y-m-d ', Strtotime (' +1 Day ')));   } return $result; /** * Send SMS to designated mobile phone number * @param $mobile * @param $content * @return BOOL */Public function Send ($mobile, $conten  T) {//TODO invoke specific service provider API return true; }/** * Determines whether it is a legitimate mobile number * @param $mobile * @return BOOL */Private Function IsMobile ($mobile) {if (Preg_match ('/^    1\d{10}$/', $mobile)) return true;  return false;  /** * Verify 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 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); }}

To pay other users to implement the SMS Verification Code codes

<?/*--------------------------------Features: China SMS network PHP HTTP interface send SMS Modified: 2009-04-08 Description: http://http.c123.com/tx/?uid= User account &AMP;PWD=MD5 bit 32 password &mobile= number &content= content status: 100 Send Success 101 authentication failed 102 SMS less than 103 operation failed 104 illegal character 105 content too 106th code too More than 107 frequency too fast 108th code content empty 109 account Freeze 110 prohibit frequent single send 111 system tentative send 112th code incorrect 120 system upgrade--------------------------------*/$uid = ' 9   999 ';   User account $pwd = ' 9999 ';  Password $mobile = ' 13912341234,13312341234,13512341234,02122334444 ';    Number $content = ' China SMS network PHP HTTP interface '; Content//Instant Send $res = Sendsms ($uid, $pwd, $mobile, $content); Echo $res; Timed Send/* $time = ' 2010-05-27 12:11 '; $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 bit 32 password ' mobile ' = $mobi Le,//number ' content ' + $content,//content ' time ' = $time,//timed to send ' mid ' + $mid//Sub-extension number);     $re = Postsms ($http, $data);  Post method Submit if (trim ($re) = = ' + ') {return ' sent successfully! '; } else {return ' send failed!  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)." & ";  Go to URL 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\r\n";       $out. = $post;    Fwrite ($fp, $out);    while (!feof ($fp)) {$receive. = fgets ($fp, 128);    } fclose ($FP);    $receive = Explode ("\r\n\r\n", $receive);    unset ($receive [0]); Return implode ("",$receive); }}?>

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP methods for working with string functions

PHP Methods for directory operations

PHP uses the Snoopy class to implement the page crawl method

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.