精品人人槡人妻人人槡,无码午夜国产视频,日韩精品无码有码视频,国产成人精品日本亚洲成熟

<var id="lf53i"><track id="lf53i"><ins id="lf53i"></ins></track></var>

  • <rp id="lf53i"></rp>
    <var id="lf53i"><track id="lf53i"><ins id="lf53i"></ins></track></var>

    <video id="lf53i"></video>
    <b id="lf53i"><acronym id="lf53i"></acronym></b>

      1. 0712-2888027 189-8648-0214
        微信公眾號

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

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

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

        時間:2018-07-20來源:風(fēng)信官網(wǎng) 點擊: 1176次
        手機(jī)注冊驗證碼操作思路與流程

        1、前端傳入手機(jī)號參數(shù)并做驗證碼倒計時

        1. /**
        2. * 重新獲取驗證碼倒計時
        3. * @returns
        4. */
        5. reGetSMS : function () {
        6. var obj = $('#btn_getCode');
        7. // 重新發(fā)送倒計時
        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ī)生成驗證碼
        1. public static String getSMSCode() {
        2. return String.valueOf((int)(Math.random() * 9000) + 1000);
        3. }
        3、將生成的驗證碼通過第三方接口已短信形式發(fā)送給手機(jī)
        1. /**
        2. *參數(shù)是手機(jī)號碼和由驗證碼組成的字符串
        3. */
        4. private static boolean send(String phone, String content) throws Exception {
        5.  
        6. // 創(chuàng)建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追加手機(jī)號碼
        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、保存驗證碼到數(shù)據(jù)庫

        要點: a、需要存的參數(shù)手機(jī)號、驗證碼、開始時間、結(jié)束時間

        1. public class SMSDto {
        2.  
        3. /** 手機(jī)號碼 */
        4. private String phone;
        5. /** 短信驗證碼 */
        6. private String sms_code;
        7. /** 開始時間(當(dāng)前秒數(shù)) */
        8. private String begin_time;
        9. /** 到期時間(當(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. * 生成驗證碼
        21. * @param phone 手機(jī)
        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、先驗證手機(jī)號碼是否存在,存在則修改

        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{ //已過期 }

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

        1. //準(zhǔn)備驗證碼
        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.發(fā)送驗證碼
        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類關(guān)鍵代碼:
        1. public class SMSUtil {
        2.  
        3. /** 短信模板 */
        4. private static final String CONTENT_0 = "(驗證碼)感謝您的支持,祝您生活愉快!【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ā)送驗證碼
        14. * @param phone 手機(jī)
        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. * 發(fā)送短信
        34. * @param phone 手機(jī)號碼
        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對象用來操作字符串
        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追加手機(jī)號碼
        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. }
        熱門關(guān)鍵詞: php 手機(jī)注冊 驗證碼
        欄目列表
        推薦內(nèi)容
        熱點內(nèi)容
        展開