PHP to the app to write SMS verification code using Memcache cache verification code

Source: Internet
Author: User

No more nonsense. Directly on the code directly using the session to store SMS verification code, the app has been said to get not to register the time has been prompted empty back to think about it or use the cache bar on the code:

//Send Verification Code method Public functionSendsmscodeop () {require_once(Base_root_path. DS. ' Api/framework/function/sms.php '); require_once(Base_root_path. DS. ' Api/framework/function/common.php '); $sms=NewSMS (); $_code=Rand(100000, 999999); $_mobile=$_post[' Mobile ']; $_content= "Hundred City Network" Welcome to register Hundred City Network, your verification code is ".$_code; if(Empty($_mobile)){            $this->_message (Code_error, ' mobile phone number cannot be empty ')); }Else{            if(!ismobile ($_mobile)){                $this->_message (code_error, ' cell phone number format is wrong ')); }Else{                if($sms->sendsms ($_content,$_mobile)){                   //$_session[' sms_code '] = $_code; $_session[' mobile ' = $_mobile;This step is written to the cache, because the server does not have Memcache installed, using the framework of the cache mechanism to store$cache= Cache::getinstance (C (' Cache.type ')); $code _info=Array(); $code _info[' last_access '] = Time(); $code _info[' sms_mobile '] =$_mobile; $code _info[' sms_mobile_code '] =$_code; $cache->set ($_mobile,$code _info); $this->_printjson (Code_success,Array("Success" =>1), ' SMS Verification code sent successfully '); }Else{                    $this->_message (Code_error, ' Verification code send failed '); }            }        }    }




    /** * @param $mobile * @param $mobile _code * Verify that the verification code is correct*/    Private  functionCheckcode ($mobile,$mobile _code){    $obj _cache= Cache::getinstance (C (' Cache.type ')); $obj=$obj _cache->get ($mobile); if(Empty($obj)){        return Array(' Error ' = ' You have not sent a verification code '); }Else{        if(!isset($obj[' last_access ']) | | ( Time()-$obj[' last_access ']) >180*1000){            return Array(' ERROR ' = ' Verification code has expired '); }Else{            if($mobile _code==$obj[' Sms_mobile_code '] &&$mobile==$obj[' Sms_mobile '] ){                return Array("Success" = ' Verify through '); }Else{                return Array(' ERROR ' = ' Verification Code wrong '); }        }    }}



PHP to the app to write SMS verification code using Memcache cache verification code

Related Article

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.