会员注册短信验证码收发

发布时间:2015-10-23 16:11 | 人气数:474

短信发送接口文件:

<?php
session_start();
if($_POST['ajax'] == 'ajax') {  //判断是否是AJAX请求
        function sendSMS($phone,$content,$verify)
        {
            $data = array
                (
                'action'=>'sendOnce',  //发送类型 
                'ac'=>'1001@501063320001EC',    //用户账号
                'cgid'=>'3360',
                'authkey'=>'1395DD077CBA8B7E041F71F01117EB2EEC',    //认证密钥
                'c'=>$content,
                'm'=>$phone          //号码,多个号码用逗号隔开
                );

            $xml= postSMS('http://smsapi.c123.cn/OpenPlatform/OpenApi',$data);                               //POST方式提交

            $re=simplexml_load_string(utf8_encode($xml));
            if(trim($re['result'])==1)  //发送成功 
            {
                $time = time();
                $phone_verify = array("phone"=>$phone,"time"=>$time,'verify'=>$verify);
                $_SESSION['phoneVerify'.$phone] = serialize($phone_verify);
                echo 1; 
            }
            else  //发送失败的返回值
            {
                echo 0;
            }
            
        }       

        function postSMS($url,$data='')
        {
            $row = parse_url($url);
            $host = $row['host'];
            $port = 80;
            $file = $row['path'];
            $post = '';
            while (list($k,$v) = each($data)) 
            {
                $post .= rawurlencode($k)."=".rawurlencode($v)."&"; //转URL标准码
            }
            $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.0\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);
            }
        }
        $verify = rand(100000,999999);
        $content = '您的手机验证码为:'.$verify.',有效期为10分钟。请及时输入!';      
        $phone= $_POST['phone'];
        sendSMS($phone,$content,$verify);       
    }

?>

前端展示部分:

<li><input name='phone' type='text' id='phone' maxlength='30' class="input_phone" placeholder="请输入您的手机号码"></li>
<li id="rekey"><input name='key' type='text' id='key' maxlength='30' class="input_key" placeholder="请输入验证码"><span class="rightts"><input type="button" value="获取验证码" id='getVerify' /></span></li>
JS代码部分:
<script type="text/javascript">
    var verify = document.getElementById('getVerify');
    verify.onclick = function(){        
        var phone = document.getElementById('phone').value;
        var telReg = !!phone.match(/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/);
        var t = 60;
        if(telReg == false){
            alert('手机号码输入错误!请重新输入。');
        }else{
            document.userinfoform.getVerify.disabled = true;
            for(i=1;i<=t;i++) {
                window.setTimeout("update_time(" + i + ","+t+")", i * 1000);
            }
            $.ajax({
                url:'/e/class/smsapi.php',
                type:'POST',
                data:{"phone":phone,"ajax":"ajax"},
                async:false,
                cache:false,
                success:function(data){
                    if(data == 1){
                        alert('验证码已发送到你的手机,请及时填写!');
                    }else{
                        alert('短信发送失败,查看手机是否输入错误,如无误,请联系我们!');
                    }
                }
            })
        }
    }
    function update_time(num,t) {
        if(num == t) {
            document.userinfoform.getVerify.value =" 重新发送 ";
            document.userinfoform.getVerify.disabled=false;
        }
        else {
            var printnr = t-num;
            document.userinfoform.getVerify.value = " (" + printnr +")秒后重发";
        }
    }
</script>
PHP后台操作部分:
/*手机验证码*/
session_start();
$sessarr = "phoneVerify".$phone;
$sessar = unserialize($_SESSION[$sessarr]);
$phone_sms = $sessar[phone];    //发送手机号
$time_sms = $sessar[time];      //短信发送时间
$verify_sms = $sessar[verify];  //短信验证码
if((time() - $time_sms)>600){
    printerror("EmptyMember_yzmtime","history.go(-1)",1);
}
if($key != $verify_sms){
    printerror("EmptyMember_yzm","history.go(-1)",1);
}
unset($_SESSION[$sessarr]);

关键词:帝国CMS, 会员注册, 手机验证, 验证码, 登陆验证