0712-2888027 189-8648-0214
微信公众号

孝感风信网络科技有限公司微信公众号

当前位置:主页 > 技术支持 > PHP > php详解手机注册验证码操作思路与流程

php详解手机注册验证码操作思路与流程

时间:2024-03-29来源:风信官网 点击: 907次
手机注册验证码操作思路与流程

1、前端传入手机号参数并做验证码倒计时

  1. /**
  2. * 重新获取验证码倒计时
  3. * @returns
  4. */
  5. reGetSMS : function () {
  6. var obj = $('#btn_getCode');
  7. // 重新发送倒计时
  8. var validCode = true;
  9. var time=60;
  10. if (validCode) {
  11. validCode = false;
  12. var t = setInterval(function () {
  13. time --;
  14. $(obj).html('重新获取('+time+'s)');
  15. if (time==0) {
  16. clearInterval(t);
  17. $(obj).html("重新获取");
  18. validCode = true;
  19. sms_flag = true;
  20. }
  21. },1000);
  22. }
  23. }
2、随机生成验证码
  1. public static String getSMSCode() {
  2. return String.valueOf((int)(Math.random() * 9000) + 1000);
  3. }
3、将生成的验证码通过第三方接口已短信形式发送给手机
  1. /**
  2. *参数是手机号码和由验证码组成的字符串
  3. */
  4. private static boolean send(String phone, String content) throws Exception {
  5.  
  6. // 创建StringBuffer对象用来操作字符串
  7. StringBuffer sb = new StringBuffer("http://http.yunsms.cn/tx/?");
  8. // 向StringBuffer追加用户名
  9. sb.append("uid=56262");
  10. // 向StringBuffer追加密码(密码采用MD5 32位 小写)
  11. sb.append("&pwd=3019654cd7d57f8a8464e2b63f8c923c");
  12. // 向StringBuffer追加手机号码
  13. sb.append("&mobile=" + phone);
  14. // 向StringBuffer追加消息内容转URL标准码
  15. sb.append("&content=" + URLEncoder.encode(content,"gbk"));
  16. BufferedReader in = null;
  17. URL url = null;
  18. HttpURLConnection connection = null;
  19. int result = 0;
  20. try {
  21. url = new URL(sb.toString());
  22. connection = (HttpURLConnection) url.openConnection();
  23. connection.setRequestMethod("POST");
  24. in = new BufferedReader(new InputStreamReader(url.openStream()));
  25. result = Integer.parseInt(in.readLine());
  26. } catch (Exception e) {
  27. throw new Exception("发送短信失败",e);
  28. } finally {
  29. if (in != null) {
  30. in.close();
  31. }
  32. if (connection != null) {
  33. connection.disconnect();
  34. }
  35. }
  36. return result == SUCCESS_SMS;
  37. }
4、保存验证码到数据库

要点: a、需要存的参数手机号、验证码、开始时间、结束时间

  1. public class SMSDto {
  2.  
  3. /** 手机号码 */
  4. private String phone;
  5. /** 短信验证码 */
  6. private String sms_code;
  7. /** 开始时间(当前秒数) */
  8. private String begin_time;
  9. /** 到期时间(当前秒数 + 有效期) */
  10. private String end_time;
  11.  
  12. /**
  13. * 默认构造方法
  14. */
  15. public SMSDto() {
  16. super();
  17. }
  18.  
  19. /**
  20. * 生成验证码
  21. * @param phone 手机
  22. * @param sms_code 验证码
  23. */
  24. public SMSDto(String phone, String sms_code) {
  25. super();
  26. this.phone = phone;
  27. this.sms_code = sms_code;
  28. int cur = (int) (System.currentTimeMillis() / 1000);
  29. this.begin_time = String.valueOf(cur);
  30. this.end_time = String.valueOf(cur + GlobalContract.TIME_SMS);
  31. }
  32. }
b、先验证手机号码是否存在,存在则修改

5、验证码验证
// 1.验证【验证码】 SMSDto smsDto = smsUserDao.getSMSCode(phone); a、验证验证码是否正确 sms_code.equals(smsDto.getSms_code()) b、验证验证码是否过期 if (((long) (System.currentTimeMillis() / 1000)) < Long.parseLong(smsDto.getEnd_time())) { //未过期 }else{ //已过期 }

实现层关键代码:

  1. //准备验证码
  2. private ResultVO sendSmsCode(String phone) throws Exception{
  3. log.info(GlobalContract.LOG_BEGIN);
  4. ResultVO resultVO = null;
  5.  
  6. // 1.生成验证码
  7. String random = SMSUtil.getSMSCode();
  8. // 2.发送验证码
  9. if(SMSUtil.sendSMS(phone, random)){
  10. // 3.保存验证码
  11. SMSDto sms = new SMSDto(phone, random);
  12. SMSDto smsDto = smsUserDao.getSMSCode(phone);
  13. if (smsDto == null) {
  14. // 新增验证码
  15. smsUserDao.addUserSMS(sms);
  16. } else {
  17. // 修改验证码
  18. smsUserDao.updateUserSMS(sms);
  19. }
  20.  
  21. resultVO = new ResultVO();
  22. } else {
  23. resultVO = new ResultVO(GlobalMessage.MSG_07);
  24. }
  25. log.info(GlobalContract.LOG_END);
  26. return resultVO;
  27. }
SMSUtil类关键代码:
  1. public class SMSUtil {
  2.  
  3. /** 短信模板 */
  4. private static final String CONTENT_0 = "(验证码)感谢您的支持,祝您生活愉快!【xxx】";
  5. /** SMS发送成功 */
  6. public static final int SUCCESS_SMS = 100;
  7.  
  8. // public static void main(String[] args) throws Exception {
  9. // System.out.println(sendSMS("18018025014", "123456"));
  10. // }
  11.  
  12. /**
  13. * 发送验证码
  14. * @param phone 手机
  15. * @param random 验证码
  16. * @return
  17. */
  18. public static boolean sendSMS(String phone, String random) throws Exception {
  19.  
  20. return send(phone, random.concat(CONTENT_0));
  21. }
  22.  
  23. /**
  24. * 生成验证码
  25. * @return
  26. */
  27. public static String getSMSCode() {
  28.  
  29. return String.valueOf((int)(Math.random() * 9000) + 1000);
  30. }
  31.  
  32. /**
  33. * 发送短信
  34. * @param phone 手机号码
  35. * @param content 发送内容
  36. * @return
  37. */
  38. private static boolean send(String phone, String content) throws Exception {
  39.  
  40. // 创建StringBuffer对象用来操作字符串
  41. StringBuffer sb = new StringBuffer("http://http.yunsms.cn/tx/?");
  42. // 向StringBuffer追加用户名
  43. sb.append("uid=56262");
  44. // 向StringBuffer追加密码(密码采用MD5 32位 小写)
  45. sb.append("&pwd=3019654cd7d57f8a8464e2b63f8c923c");
  46. // 向StringBuffer追加手机号码
  47. sb.append("&mobile=" + phone);
  48. // 向StringBuffer追加消息内容转URL标准码
  49. sb.append("&content=" + URLEncoder.encode(content,"gbk"));
  50. BufferedReader in = null;
  51. URL url = null;
  52. HttpURLConnection connection = null;
  53. int result = 0;
  54. try {
  55. url = new URL(sb.toString());
  56. connection = (HttpURLConnection) url.openConnection();
  57. connection.setRequestMethod("POST");
  58. in = new BufferedReader(new InputStreamReader(url.openStream()));
  59. result = Integer.parseInt(in.readLine());
  60. } catch (Exception e) {
  61. throw new Exception("发送短信失败",e);
  62. } finally {
  63. if (in != null) {
  64. in.close();
  65. }
  66. if (connection != null) {
  67. connection.disconnect();
  68. }
  69. }
  70. return result == SUCCESS_SMS;
  71. }
  72.  
  73. }
热门关键词: php 手机注册 验证码
栏目列表
推荐内容
热点内容
展开