Home >CMS Tutorial >DEDECMS >How to implement the SMS verification code function of Dreamweaver

How to implement the SMS verification code function of Dreamweaver

藏色散人
藏色散人Original
2019-12-13 09:50:303177browse

How to implement the SMS verification code function of Dreamweaver

How to implement the Dreamweaver SMS verification code function?

Most websites now require SMS verification codes. Because DreamWeaver officially does not have an SMS verification code plug-in, we wrote several SMS verification code plug-ins. One of them uses Alibaba Cloud's SMS verification code. Interfaces, one uses Alibaba's SMS verification code interface, one uses Alibaba Communications' SMS verification code interface, and the other uses Yunzhixun's SMS interface.

Recommended learning: DreamWeaver cms

The following tutorial contains 2 Dreamweaver SMS verification code interfaces.

Things that need to be modified in the SMS registration of Dreamweaver members:

1. It is necessary to create a table to record the verification code to prevent multiple sending. Here, a phonecode table is created.

2. member/templets/reg-new.htm (Add elements to registration template)

3. member/templets/js/reg_new.js (Verify mobile phone number)

4. member/index_do.php (Determine whether to send the registration verification code according to the background settings)

5. member/reg_new.php (verification, record)

a. Use Alibaba Cloud SMS Interface access:

You need to insert the Alibaba Cloud SMS interface code at the corresponding location of member/index_do.php. It is particularly important to note that the official demo document of Alibaba Cloud SMS is written by a master, so ordinary people will report namespace errors when using it. This needs to be paid attention to. In addition, the SMS template needs to be updated. Now Alibaba SMS template review is very strict and other variables are not allowed. This problem did not exist before. Therefore, when the audit fails, variables need to be reduced. (Currently it is no longer possible to open the Alibaba Cloud SMS interface. The newly opened Alibaba Cloud SMS interface cannot use this code. It is only suitable for those who have opened Alibaba Cloud SMS a long time ago)

function getrandchar($length){
    $str = null;
    $strPol = "0123456789abcdefghijklmnopqrstuvwxyz";
    $max = strlen($strPol)-1;
        for($i=0;$i<$length;$i++){
        $str.=$strPol[rand(0,$max)];
        }
        return $str;
    }
    $code = getrandchar(5);
  
    require_once(DEDEINC.&#39;/aliyun-php-sdk-core/Config.php&#39;);
    use Sms\Request\V20160927 as Sms;
    $iClientProfile = DefaultProfile::getProfile("cn-shenzhen", "your accessKey", "your accessSecret"); //登录阿里云查看: "your accessKey", "your accessSecret"
    $client = new DefaultAcsClient($iClientProfile);   
    $request = new Sms\SingleSendSmsRequest();
    $request->setSignName("签名");/*签名名称*/
    $request->setTemplateCode("SMS_1111");/*模板code*/
    $request->setRecNum($phone);/*目标手机号*/
    $request->setParamString("{\"code\":\"$code\",\"tel\":\"电话号码\"}");/*模板变量,请确保跟审核过的短信模版变量一致,数字一定要转换为字符串*/
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
    }
    catch (ClientException  $e) {
        print_r($e->getErrorCode());  
        print_r($e->getErrorMessage());  
    }
    catch (ServerException  $e) {  
        print_r($e->getErrorCode());  
        print_r($e->getErrorMessage());
    }
      
    $inquery = " INSERT INTO `dede_phonecode` (`ip`,`phone`,`phonecode`,`used`,`sendtime`) VALUES ( &#39;$ip&#39;,&#39;$to&#39;,&#39;$code&#39;,&#39;1&#39;,&#39;$nowtime&#39;); ";
    $rs = $dsql->ExecuteNoneQuery2($inquery);
    if( $rs = 1 ){
        ShowMsg(&#39;发送成功,请注意查收!&#39;,&#39;-1&#39;);
        exit();
    }
    exit();

Attached is the Alibaba Cloud SMS template : Verification code: ${code}. You are registering. If it is not done by you, please ignore this message. If you have any questions, please contact us! Telephone: ${tel}

b. Use Alibaba Dayu’s SMS interface to access:

The same needs to be in the corresponding location of member/index_do.php Insert the following code.

function getrandchar($length){
$str = null;
//$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$strPol = "0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol)-1;
for($i=0;$i<$length;$i++){
$str.=$strPol[rand(0,$max)];
}
return $str;
}
$code = getrandchar(5);
  
require_once(DEDEINC.&#39;/alidayu-php-sdk/TopSdk.php&#39;);
$client = new TopClient;
$client ->appkey = &#39;111111&#39; ; //登录阿里大于查看appkey。
$client ->secretKey = &#39;aaaaaaaaaaaaaa&#39; ; //登录阿里大于查看secret。
$req = new AlibabaAliqinFcSmsNumSendRequest;
$req ->setExtend( "" );
$req ->setSmsType( "normal" );
$req ->setSmsFreeSignName( "城子居" ); /*签名名称*/
$req ->setSmsParam( "{\"code\":\"$code\",\"name\":\"注册\"}" ); /*模板变量,请确保跟审核过的短信模版变量一致,数字一定要转换为字符串*/
$req ->setRecNum($phone);/*目标手机号*/
$req ->setSmsTemplateCode( "SMS_1111111" );//登录阿里大于查看/*模板ID编号*/
$resp = $client ->execute( $req );
  
$inquery = " INSERT INTO `imm_phonecode` (`ip`,`phone`,`phonecode`,`used`,`sendtime`)  VALUES ( &#39;$ip&#39;,&#39;$to&#39;,&#39;$code&#39;,&#39;1&#39;,&#39;$nowtime&#39;); ";
$rs = $dsql->ExecuteNoneQuery2($inquery);
if( $rs = 1 ){
ShowMsg(&#39;发送成功,请注意查收!&#39;,&#39;-1&#39;);
exit();
}
exit();

c. Use the latest Alibaba Communication SMS interface to access:

function getrandchar($length){
$str = null;
//$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$strPol = "0123456789";
$max = strlen($strPol)-1;
for($i=0;$i<$length;$i++){
$str.=$strPol[rand(0,$max)];
}
return $str;
}
$number = getrandchar(4);
  
require_once(DEDEINC.&#39;/aliyun-php-sdk-core/Config.php&#39;);
require_once(DEDEINC.&#39;/Dysmsapi/Request/V20170525/SendSmsRequest.php&#39;);
require_once(DEDEINC.&#39;/Dysmsapi/Request/V20170525/QuerySendDetailsRequest.php&#39;);
  
function sendSms() {
global $phone, $number;
  
//此处需要替换成自己的AK信息
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
//短信API产品名
$product = "Dysmsapi";
//短信API产品域名
$domain = "dysmsapi.aliyuncs.com";
//暂时不支持多Region
$region = "cn-beijing";
  
//初始化访问的acsCleint
$profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
DefaultProfile::addEndpoint("cn-beijing", "cn-beijing", $product, $domain);
$acsClient= new DefaultAcsClient($profile);
  
$request = new Dysmsapi\Request\V20170525\SendSmsRequest;
//必填-短信接收号码
$request->setPhoneNumbers($phone);
//必填-短信签名
$request->setSignName("99商铺网");
//必填-短信模板Code
$request->setTemplateCode("SMS_74725029");
//选填-假如模板中存在变量需要替换则为必填(JSON格式)
$request->setTemplateParam("{\"number\":\"$number\"}");
//选填-发送短信流水号
$request->setOutId("1234");
  
//发起访问请求
$acsResponse = $acsClient->getAcsResponse($request);
}
  
sendSms();
  
$inquery = " INSERT INTO `imm_phonecode` (`ip`,`phone`,`phonecode`,`used`,`sendtime`)  VALUES ( &#39;$ip&#39;,&#39;$phone&#39;,&#39;$number&#39;,&#39;1&#39;,&#39;$nowtime&#39;); ";
$rs = $dsql->ExecuteNoneQuery2($inquery);
if( $rs = 1 ){
echo "发送成功,请注意查收!";
exit();
}
exit();

Here are all the three SMS interfaces of Alibaba. The corresponding SDK needs to be found in the official Alibaba Cloud download.

d. Use Yunzhixun SMS interface to access:

Similarly, you need to insert the Yunzhixun SMS interface code in the corresponding position of member/index_do.php.

function getrandchar($length){
$str = null;
$strPol = "0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol)-1;
for($i=0;$i<$length;$i++){
$str.=$strPol[rand(0,$max)];
}
return $str;
}
require_once(DEDEINC.&#39;/ucpaas.class.php&#39;);
$options[&#39;accountsid&#39;]=&#39;&#39;;  //对应ucpaas.com用户ID
$options[&#39;token&#39;]=&#39;&#39;;  //对应ucpaas.com里面的用户token
$ucpass = new Ucpaas($options);
$appId = ""; //对应ucpaas.com里面的项目ID
$to = $phone;
$templateId = ""; //对应ucpaas.com里面的短信模版ID
$code = getrandchar(5);
$param= $code.&#39;,短信模版参数2&#39;.&#39;,短信模版参数3&#39;; // $code为生成的验证码,短信模版参数2,短信模版参数3,参数之间用英文逗号间隔。
$ucpass->templateSMS($appId,$to,$templateId,$param);
  
$inquery = " INSERT INTO `dede_phonecode` (`ip`,`phone`,`phonecode`,`used`,`sendtime`) VALUES ( &#39;$ip&#39;,&#39;$to&#39;,&#39;$code&#39;,&#39;1&#39;,&#39;$nowtime&#39;); ";
$rs = $dsql->ExecuteNoneQuery2($inquery);
if( $rs = 1 ){
ShowMsg(&#39;发送成功,请注意查收!&#39;,&#39;-1&#39;);
exit();
}
exit();

Similarly attach the SMS template: Verification code: {1}. You are at {2}. If you are not responsible for this, please ignore this message. If you have any questions, please contact us! Tel: {3}

The above is the detailed content of How to implement the SMS verification code function of Dreamweaver. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn