下载附件代码解压到项目中,引用配置文件和类文件。
这是自己写的使用方法,ajax发送验证码(包含倒计时)
一、前台代码
<div class="code1"> <label>验证码:</label> <input type="text" name="reg_code" class="code" id="regcode"> <input type="button" id="btn" value="获取验证码" onclick="getCode();settime(this);" disabled="disabled" /> </div> <script> var countdown=60; function settime(obj) { if (countdown == 0) { $('#btn').css('background','#567ed2') obj.removeAttribute("disabled"); obj.value="获取验证码"; countdown = 60; return; } else { $('#btn').css('background','#ddd') obj.setAttribute("disabled", true); obj.value="重新发送(" + countdown + ")"; countdown--; } setTimeout(function() { settime(obj) },1000) } function getCode(){ var phone = $("#regphone").val(); $.ajax({ data:{'phone':phone,'status':'code'}, type:'post', url :"/api.php", success:function(obj){ if (obj == '1') { alert('获取验证码太频繁!'); } if (obj == '2') { alert('验证码发送失败,请稍后再试!'); } if (obj == '3') { alert('数据库错误!'); } if (obj == 'success') { alert('发送成功!'); } }, error:function(obj){ } }); } </script>
二、php代码(业务逻辑根据自己需求修改)
//发送验证码 注册 if($_POST['status'] == 'code'){ $table_name = 'wp_phone_register'; global $wpdb, $table_name; // 删除过期验证码 $wpdb->query($wpdb->prepare("DELETE FROM `$table_name` WHERE `time` < %s", (time() - 600))); $phone=trim($_POST['phone']); // 上次发送短信的时间,防止短信攻击 $time = $wpdb->get_var($wpdb->prepare("SELECT `time` FROM `$table_name` WHERE `phone` = %s;", $phone)); //获取验证码太频繁 if (!empty($time) && (time() - $time)> 61){ $error = '获取验证码太频繁'; echo 1;exit();//获取验证码太频繁 } //短信验证码是否过期 // $gq_time = $wpdb->get_var($wpdb->prepare("SELECT `gq_time` FROM `$table_name` WHERE `phone` = %s;", $phone)); // if (!empty($gq_time) && (time() - $gq_time)>0){ // echo 2;exit(); // } if (empty($error)) { $code=random($length = 6 , $numeric = 1); if (empty($time)){ $db = $wpdb->insert($table_name, array('phone' => $phone, 'code' => $code, 'time' => time(),'gq_time'=>time()+600), array('%s', '%s', '%d','%d')); } else{ $db = $wpdb->update($table_name, array('code' => $code, 'time' => time(),'gq_time'=>time()+600), array('phone' => $phone), array('%s', '%d','%d'), array('%s')); } if ($db) { $send_status = sxt_send_sms($code, $phone); if($send_status == 0){ echo 2;//验证码发送失败 exit(); }else{ echo 'success'; exit(); } } else { echo 3;//数据库错误! } } else { echo 1;exit();//获取验证码太频繁 } } // 阿里大于发送短信 function send_sms($code, $phone) { require_once '/alisms/config.php'; require_once '/alisms/alisms.php'; $alisms = new SmsDemo($AccessKeyId, $AccessKeySecret); $res = $alisms->sendSms( $sign, // 短信签名 $template, // 短信模板编号 $phone, // 短信接收者 Array( // 短信模板中字段的值 "name" => $code ) ); if ($res->Code == 'OK'){ return 1; } else{ return 0; } } //random() 函数返回随机整数。 function random($length = 6 , $numeric = 0) { PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000); if($numeric) { $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1)); } else { $hash = ''; $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz'; $max = strlen($chars) - 1; for($i = 0; $i < $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } } return $hash; }
三、修改配置信息(config.php)
<?php // 阿里云 Access Key Id $AccessKeyId = ''; // 阿里云 Access Key Secret $AccessKeySecret = ''; // 短信签名 $sign = ''; // 短信模板CODE,如 SMS_123456789 $template = '';