0

    织梦短信验证码功能怎么实现

    2023.05.21 | admin | 161次围观

    织梦短信验证码功能怎么实现?

    现在大部分网站都需要用短信验证码,因为织梦官方没有短信验证码插件,所以写了几个短信验证码插件,一个使用的是阿里云的短信验证码接口,一个使用的是阿里大于的短信验证码接口,一个使用的是阿里通信短信验证码接口,另外一个使用的是云之讯的短信接口。

    推荐学习:织梦cms

    下面的教程包含2个织梦短信验证码接口。

    织梦会员短信注册需要修改的地方:

    1、需要创建一个表来对验证码进行记录,防止多次发送,这里创建了一个phonecode表。

    2、member/templets/reg-new.htm (注册模版添加元素)

    3、member/templets/js/reg_new.js (验证手机号)

    4、member/index_do.php (根据后台设置,判断是否发送注册验证码)

    5、member/reg_new.php (验证、记录)

    a.使用阿里云短信接口接入:

    需要在member/index_do.php对应的位置插入阿里云短信接口代码。特别需要注意的是,阿里云短信官方demo文档是大神写的,所以普通人用起来会报命名空间错误,这个需要自己注意。另外需要更新下短息模板,现在阿里短信模板审核非常严格,不允许有其他变量,之前并没有这个问题。所以,审核不过的时候,需要减少变量。(目前已经无法开通阿里云短信接口,新开通的是阿里通信接口,不能使用这段代码dedecms没有验证码dedecms没有验证码,只适合很久之前就开通过阿里云短信的人)

    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.'/aliyun-php-sdk-core/Config.php');
        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 ( '$ip','$to','$code','1','$nowtime'); ";
        $rs = $dsql->ExecuteNoneQuery2($inquery);
        if( $rs = 1 ){
            ShowMsg('发送成功,请注意查收!','-1');
            exit();
        }
        exit();

    登录后复制

    附上阿里云短信模版:验证码:${code}。您正在注册,如非您本人操作,请忽略此条短信。如有疑问请与我们联系! 电话:${tel}

    b.使用阿里大鱼的短信接口接入:

    同样的需要在member/index_do.php对应的位置插入以下代码。

    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.'/alidayu-php-sdk/TopSdk.php');
    $client = new TopClient;
    $client ->appkey = '111111' ; //登录阿里大于查看appkey。
    $client ->secretKey = 'aaaaaaaaaaaaaa' ; //登录阿里大于查看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 ( '$ip','$to','$code','1','$nowtime'); ";
    $rs = $dsql->ExecuteNoneQuery2($inquery);
    if( $rs = 1 ){
    ShowMsg('发送成功,请注意查收!','-1');
    exit();
    }
    exit();

    登录后复制

    c.使用最新的阿里通信短信接口接入:

    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.'/aliyun-php-sdk-core/Config.php');
    require_once(DEDEINC.'/Dysmsapi/Request/V20170525/SendSmsRequest.php');
    require_once(DEDEINC.'/Dysmsapi/Request/V20170525/QuerySendDetailsRequest.php');
      
    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 ( '$ip','$phone','$number','1','$nowtime'); ";
    $rs = $dsql->ExecuteNoneQuery2($inquery);
    if( $rs = 1 ){
    echo "发送成功,请注意查收!";
    exit();
    }
    exit();

    登录后复制

    到这里阿里系的3个短信接口就全都在这里了,对应的SDK需要在阿里云官方查找下载。

    d.使用云之讯短信接口接入:

    同样的需要在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.'/ucpaas.class.php');
    $options['accountsid']='';  //对应ucpaas.com用户ID
    $options['token']='';  //对应ucpaas.com里面的用户token
    $ucpass = new Ucpaas($options);
    $appId = ""; //对应ucpaas.com里面的项目ID
    $to = $phone;
    $templateId = ""; //对应ucpaas.com里面的短信模版ID
    $code = getrandchar(5);
    $param= $code.',短信模版参数2'.',短信模版参数3'; // $code为生成的验证码,短信模版参数2,短信模版参数3,参数之间用英文逗号间隔。
    $ucpass->templateSMS($appId,$to,$templateId,$param);
      
    $inquery = " INSERT INTO `dede_phonecode` (`ip`,`phone`,`phonecode`,`used`,`sendtime`) VALUES ( '$ip','$to','$code','1','$nowtime'); ";
    $rs = $dsql->ExecuteNoneQuery2($inquery);
    if( $rs = 1 ){
    ShowMsg('发送成功,请注意查收!','-1');
    exit();
    }
    exit();

    登录后复制

    同样的附上短信模版:验证码:{1}。您正在{2},如非您本人操作,请忽略此条短信。如有疑问请与我们联系! 电话:{3}

    以上就是织梦短信验证码功能怎么实现的详细内容,更多请关注php中文网其它相关文章!

    版权声明

    本文仅代表作者观点。
    本文系作者授权发表,未经许可,不得转载。

    发表评论