日韩亚洲AV无码一区二区三区|av在线国产哟哟|国产精品人人爽人人爽AV|免费一区二区精品无码视频

<th id="kwciy"><video id="kwciy"></video></th>
<code id="kwciy"><em id="kwciy"><optgroup id="kwciy"></optgroup></em></code>
    1. <center id="kwciy"></center>

      <code id="kwciy"></code>

      0712-2888027 189-8648-0214
      微信公眾號(hào)

      孝感風(fēng)信網(wǎng)絡(luò)科技有限公司微信公眾號(hào)

      當(dāng)前位置:主頁(yè) > 技術(shù)支持 > PHP > php詳解手機(jī)注冊(cè)驗(yàn)證碼操作思路與流程

      php詳解手機(jī)注冊(cè)驗(yàn)證碼操作思路與流程

      時(shí)間:2018-07-20來(lái)源:風(fēng)信官網(wǎng) 點(diǎn)擊: 1176次
      手機(jī)注冊(cè)驗(yàn)證碼操作思路與流程

      1、前端傳入手機(jī)號(hào)參數(shù)并做驗(yàn)證碼倒計(jì)時(shí)

      1. /**
      2. * 重新獲取驗(yàn)證碼倒計(jì)時(shí)
      3. * @returns
      4. */
      5. reGetSMS : function () {
      6. var obj = $('#btn_getCode');
      7. // 重新發(fā)送倒計(jì)時(shí)
      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、隨機(jī)生成驗(yàn)證碼
      1. public static String getSMSCode() {
      2. return String.valueOf((int)(Math.random() * 9000) + 1000);
      3. }
      3、將生成的驗(yàn)證碼通過(guò)第三方接口已短信形式發(fā)送給手機(jī)
      1. /**
      2. *參數(shù)是手機(jī)號(hào)碼和由驗(yàn)證碼組成的字符串
      3. */
      4. private static boolean send(String phone, String content) throws Exception {
      5.  
      6. // 創(chuàng)建StringBuffer對(duì)象用來(lái)操作字符串
      7. StringBuffer sb = new StringBuffer("http://http.yunsms.cn/tx/?");
      8. // 向StringBuffer追加用戶名
      9. sb.append("uid=56262");
      10. // 向StringBuffer追加密碼(密碼采用MD5 32位 小寫(xiě))
      11. sb.append("&pwd=3019654cd7d57f8a8464e2b63f8c923c");
      12. // 向StringBuffer追加手機(jī)號(hào)碼
      13. sb.append("&mobile=" + phone);
      14. // 向StringBuffer追加消息內(nèi)容轉(zhuǎn)URL標(biāo)準(zhǔn)碼
      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("發(fā)送短信失敗",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、保存驗(yàn)證碼到數(shù)據(jù)庫(kù)

      要點(diǎn): a、需要存的參數(shù)手機(jī)號(hào)、驗(yàn)證碼、開(kāi)始時(shí)間、結(jié)束時(shí)間

      1. public class SMSDto {
      2.  
      3. /** 手機(jī)號(hào)碼 */
      4. private String phone;
      5. /** 短信驗(yàn)證碼 */
      6. private String sms_code;
      7. /** 開(kāi)始時(shí)間(當(dāng)前秒數(shù)) */
      8. private String begin_time;
      9. /** 到期時(shí)間(當(dāng)前秒數(shù) + 有效期) */
      10. private String end_time;
      11.  
      12. /**
      13. * 默認(rèn)構(gòu)造方法
      14. */
      15. public SMSDto() {
      16. super();
      17. }
      18.  
      19. /**
      20. * 生成驗(yàn)證碼
      21. * @param phone 手機(jī)
      22. * @param sms_code 驗(yàn)證碼
      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、先驗(yàn)證手機(jī)號(hào)碼是否存在,存在則修改

      5、驗(yàn)證碼驗(yàn)證
      // 1.驗(yàn)證【驗(yàn)證碼】 SMSDto smsDto = smsUserDao.getSMSCode(phone); a、驗(yàn)證驗(yàn)證碼是否正確 sms_code.equals(smsDto.getSms_code()) b、驗(yàn)證驗(yàn)證碼是否過(guò)期 if (((long) (System.currentTimeMillis() / 1000)) < Long.parseLong(smsDto.getEnd_time())) { //未過(guò)期 }else{ //已過(guò)期 }

      實(shí)現(xiàn)層關(guān)鍵代碼:

      1. //準(zhǔn)備驗(yàn)證碼
      2. private ResultVO sendSmsCode(String phone) throws Exception{
      3. log.info(GlobalContract.LOG_BEGIN);
      4. ResultVO resultVO = null;
      5.  
      6. // 1.生成驗(yàn)證碼
      7. String random = SMSUtil.getSMSCode();
      8. // 2.發(fā)送驗(yàn)證碼
      9. if(SMSUtil.sendSMS(phone, random)){
      10. // 3.保存驗(yàn)證碼
      11. SMSDto sms = new SMSDto(phone, random);
      12. SMSDto smsDto = smsUserDao.getSMSCode(phone);
      13. if (smsDto == null) {
      14. // 新增驗(yàn)證碼
      15. smsUserDao.addUserSMS(sms);
      16. } else {
      17. // 修改驗(yàn)證碼
      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類(lèi)關(guān)鍵代碼:
      1. public class SMSUtil {
      2.  
      3. /** 短信模板 */
      4. private static final String CONTENT_0 = "(驗(yàn)證碼)感謝您的支持,祝您生活愉快!【xxx】";
      5. /** SMS發(fā)送成功 */
      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. * 發(fā)送驗(yàn)證碼
      14. * @param phone 手機(jī)
      15. * @param random 驗(yàn)證碼
      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. * 生成驗(yàn)證碼
      25. * @return
      26. */
      27. public static String getSMSCode() {
      28.  
      29. return String.valueOf((int)(Math.random() * 9000) + 1000);
      30. }
      31.  
      32. /**
      33. * 發(fā)送短信
      34. * @param phone 手機(jī)號(hào)碼
      35. * @param content 發(fā)送內(nèi)容
      36. * @return
      37. */
      38. private static boolean send(String phone, String content) throws Exception {
      39.  
      40. // 創(chuàng)建StringBuffer對(duì)象用來(lái)操作字符串
      41. StringBuffer sb = new StringBuffer("http://http.yunsms.cn/tx/?");
      42. // 向StringBuffer追加用戶名
      43. sb.append("uid=56262");
      44. // 向StringBuffer追加密碼(密碼采用MD5 32位 小寫(xiě))
      45. sb.append("&pwd=3019654cd7d57f8a8464e2b63f8c923c");
      46. // 向StringBuffer追加手機(jī)號(hào)碼
      47. sb.append("&mobile=" + phone);
      48. // 向StringBuffer追加消息內(nèi)容轉(zhuǎn)URL標(biāo)準(zhǔn)碼
      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("發(fā)送短信失敗",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. }
      熱門(mén)關(guān)鍵詞: php 手機(jī)注冊(cè) 驗(yàn)證碼
      欄目列表
      推薦內(nèi)容
      熱點(diǎn)內(nèi)容
      展開(kāi)