PHP手机注册发送验证码操作思路和过程详细说明

2022-11-10 0 391

PHP手机注册发送验证码操作思路和过程详细说明

 

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、将生成的验证码通过第三方接口已短信形式发送给手机

  1. /**
  2. *参数是手机号码和由验证码组成的字符串
  3. */
  4. private static boolean send(String phone, String content) throws Exception {
  5. // 创建StringBuffer对象用来操作字符串
  6. StringBuffer sb = new StringBuffer(“http://http.yunsms.cn/tx/?”);
  7. // 向StringBuffer追加用户名
  8. sb.append(“uid=56262”);
  9. // 向StringBuffer追加密码(密码采用MD5 32位 小写)
  10. sb.append(“&pwd=3019654cd7d57f8a8464e2b63f8c923c”);
  11. // 向StringBuffer追加手机号码
  12. sb.append(“&mobile=” + phone);
  13. // 向StringBuffer追加消息内容转URL标准码
  14. sb.append(“&content=” + URLEncoder.encode(content,”gbk”));
  15. BufferedReader in = null;
  16. URL url = null;
  17. HttpURLConnection connection = null;
  18. int result = 0;
  19. try {
  20. url = new URL(sb.toString());
  21. connection = (HttpURLConnection) url.openConnection();
  22. connection.setRequestMethod(“POST”);
  23. in = new BufferedReader(new InputStreamReader(url.openStream()));
  24. result = Integer.parseInt(in.readLine());
  25. } catch (Exception e) {
  26. throw new Exception(“发送短信失败”,e);
  27. } finally {
  28. if (in != null) {
  29. in.close();
  30. }
  31. if (connection != null) {
  32. connection.disconnect();
  33. }
  34. }
  35. return result == SUCCESS_SMS;
  36. }

4、保存验证码到数据库

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

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

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. // 1.生成验证码
  6. String random = SMSUtil.getSMSCode();
  7. // 2.发送验证码
  8. if(SMSUtil.sendSMS(phone, random)){
  9. // 3.保存验证码
  10. SMSDto sms = new SMSDto(phone, random);
  11. SMSDto smsDto = smsUserDao.getSMSCode(phone);
  12. if (smsDto == null) {
  13. // 新增验证码
  14. smsUserDao.addUserSMS(sms);
  15. } else {
  16. // 修改验证码
  17. smsUserDao.updateUserSMS(sms);
  18. }
  19. resultVO = new ResultVO();
  20. } else {
  21. resultVO = new ResultVO(GlobalMessage.MSG_07);
  22. }
  23. log.info(GlobalContract.LOG_END);
  24. return resultVO;
  25. }

SMSUtil类关键代码:

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

 

 

 

 

1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!cheeksyu@vip.qq.com
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
4. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有积分奖励和额外收入!
5.严禁将资源用于任何违法犯罪行为,不得违反国家法律,否则责任自负,一切法律责任与本站无关

源码下载

发表评论
暂无评论